JAVA学习第二十九课(经常使用对象API)- String类

时间:2023-03-09 00:23:41
JAVA学习第二十九课(经常使用对象API)- String类

多线程告一段落,開始经常使用对象API的涉及,背也要背下来!。!

日后开发,遇见最多的对象是文字,也就是字符串

String类

字符串是一个特殊对象

字符串一旦初始化就不能够被改变

一、特点

public class Main {
public static void main(String[] args)
{
Demo1();
System.out.println("--------------");
Demo2();
}
/*演示字符串的第一种定义方式,并明白字符串常量池的特点*/
private static void Demo1()
{
String s1 = "asd";
s1 = "sd";//s1仅仅是字符串的引用,而字符串“asd”本身不变
String s2 = new String("sdf");
System.out.println(s1);
s2 = "sd";
System.out.println(s2==s1);//s1 和 s2实际上指向的同一个地址值
//sd储存在缓冲区(字符串常量池)
//假设没有。就创建,没有就不创建
}
/*演示字符串的另外一种定义方式,*/
public static void Demo2()
{
String s = "abc";//在字符串常量池中创建一个对象
String s1 = new String("abc");//没有在字符串常量池中创建,在堆内存创建对象两个对象
//一个是s1 一个“abc”
System.out.println(s == s1);//false,比較的地址值不一样
System.out.println(s.equals(s1));//true
/*String类中的equals复写了Object中的equals建立了String类
* 自己的推断字符串对象是否同样
* 也就是比較字符串内容*/
System.out.println("s = "+s);
System.out.println("s1 = "+s1);
}
}



二、构造函数

public class Main
{
public static void main(String[] args)
{
StringconstructorDemo1();
charDemo();
}
public static void StringconstructorDemo1()
{
String s = new String(); // 等效于String s = "";地址值不一样,不等效String s = null
byte[] sub = {97,66};
String s1 = new String(sub);
System.out.println("s1 = "+s1);
}
public static void charDemo()
{
char[] ch = {'a','b','c','d','e','f'};
String string = new String(ch);//将数组变成字符串,仅仅能是byte 和 char
System.out.println("string = "+string); String string1 = new String(ch,0,3);//从0角标开是截取3个字符
System.out.println("string1 = "+string1);
//String类剩下的方法,查手冊
}

三、字符串涉及的方法

全然以代码演示

/*
* 需求:
* 依照面向对象的思想学习字符串的方法
* "abcdefg"
* 1.字符串的长度
* 2.d在字符串的位置
* 3.....
* */
public class Main
{
public static void main(String[] args)
{
//StringMthodDemo1();//获取
//StringMthodDemo2();//转换
//StringMthodDemo3();//推断
StringMthodDemo4();//比較
} public static void StringMthodDemo1()//一、获取
{
String str = "abcdefghijk"; /* 1. 获取字符串长度 int length() */
int len = str.length();
System.out.println("str len = "+len); /* 2. 依据位置获取字符 char charAt(int index) */
char wz = str.charAt(3);
System.out.println("索引为3的字符 "+wz); /* 3. 依据字符获取字符所在的位置 indexOf (重点)*/ int iwz = str.indexOf('x');//写'a' 也行,写97也行(获取參数第一次出现的位置)
System.out.println("a 的位置"+iwz);//-1表示不存在 //3.1重载形式
int iwz1 = str.indexOf('d',2);//返回在此字符串中第一次出现指定字符串处的索引。从指定的索引開始搜索
System.out.println("d在字符串的位置 "+iwz1);
//3.2重载形式
int iwz2 = str.indexOf("cd");//返回指定字符串在此字符串中第一次出现指定字符串处的索引
System.out.println("字符串cd第一次出现的位置 "+iwz2);
//3.3
int iwz3 = str.indexOf("cd",1); //lastindexOf()//返回在此字符串中第最后一次出现指定字符串处的索引,同上(必须会) /* 4. 截取部分子串 substring(int beginIndex,int beginIndex) */
String sub = str.substring(0,3);//从 beginIndex 到 “beginIndex-1” 处的子串
System.out.println("索引从0-2的子串 "+sub);
//..........手冊
}
public static void StringMthodDemo2()//二、转换
{
String str = "abc,efg,hij,lmn,opq,rst"; /* 1. 将字符串变成字符串数组(也就是将字符串切割成若干段)split */
String[] arr = str.split(",");//以“ ,”为规则切割。涉及到正則表達式
for(String i : arr)
{
System.out.println(i);
}
/* 2. 将字符串变成字符数组 (切割成单个字符) toCharArray*/ char[] arr1 = str.toCharArray();
for(char i : arr1)
{
System.out.println(i);
} /* 3. 将字符串变成字节数组 etBytes*/
String str1 = "abc阿萨德";//abc 3个字节。三个字 6个字节
//ASCII是美国的,中国也有自己的GB2312 相应的二进制码表
byte[] g = str.getBytes();
for(byte i : g)
{
System.out.println(i);
} /* 4. 字母大写和小写相互转换 toUpperCas */ String str2 = "asd";
String str3 = "AAAA";
String t1 = str2.toUpperCase();//变大写
String t2 = str3.toLowerCase();//变小写
System.out.println("UP "+t1 +"++++ "+" Lower "+t2); /* 5. 字符替换 replace */ String str4 = "asfsd";
String t3 = str4.replace('s', 'a');//''如果s不存在。就无结果。返回原串
System.out.println("字符替换"+t3);
String t4 = str4.replace("asf", "aaa");
System.out.println("子串替换"+t4); /* 6. 去除字符串两端空格 trim (实用) */
//比方说发短信给10086 有时候习惯性的按一个空格,可是不支持这个空格。所以trim去除这个空格
System.out.println(" abcd ".trim()); /* 7. 将字符串进行连接 */ System.out.println("asd".concat("asd"));//concat的核心代码就是 "asd"+"asd"
/* 8. */
System.out.println(String.valueOf(25)+0);//将25转换为字符串 再与0连接 -> println(""+25+0);
} public static void StringMthodDemo3()//三、推断
{
/* 1. equals */
String Str1 = "asd";
String str2 = "ASD";
boolean flag = Str1.equals(str2);
System.out.println(flag); /* 2. equalsIgnoreCase */
System.out.println(Str1.equalsIgnoreCase(str2));//忽略大写和小写对字符串的影响
//相当于 Str1.equals(str2.toLowerCase()); 当然比較烂 /* 3. 字符串中是否包括指定字符串 contains*/ System.out.println("asdfghj".contains("fgh")); /* 4. 字符串是否以指定字符串开头或结尾 startsWith endsWith */ System.out.println("asdfghjk".startsWith("asd")+"......"+"asdfghjk".endsWith("jkl")); /* ...... */
}
public static void StringMthodDemo4()//四、比較
{
//依照字典序比較 compareTo
String str1 = "abc";
String str2 = "efg";
System.out.println(str2.compareTo(str1));//等于0 :str1相等str2 , 大于0 :str1大于str2,小于0: str1小于str2 }
}

都是死东西。背也要背下来