String类提供的构造方式非常多,常用的就以下三种:
public static void main(String[] args) {
//使⽤字符串常量进⾏赋值
String s1 = "hello bit";
System.out.println(s1);
//直接new String对象
String s2 = new String("hello bit");
System.out.println(s2);
//使⽤字符数组进⾏构造
char[] array = {'h','e','l','l','o','b','i','t'};
String s3 = new String(array);
System.out.println(s3);
//使⽤字节数组构造对象
byte[] bytes = {97,98,99,100};
String s4 = new String(bytes);
System.out.println(s4);
}#注:String是引用类型,内部并不存储字符串本身。
(1)==比较是否引用同⼀个对象
#注:对于内置类型,==比较的是变量中的值;对于引用类型==⽐较的是引用中的地址。
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
// 对于基本类型变量,==⽐较两个变量中存储的值是否相同
System.out.println(a == b); // false
System.out.println(a == c); // true
// 对于引⽤类型变量,==⽐较两个引⽤变量引⽤的是否为同⼀个对象
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("world");
String s4 = s1;
System.out.println(s1 == s2); // false
System.out.println(s2 == s3); // false
System.out.println(s1 == s4); // true
}(2)equals ⽅法:按照字典序比较(字典序:字符大小的顺序)
String类重写了⽗类Object中equals⽅法,比较时,每一位一次比较,直到不同后得到结果,全部相等才返回true
(3)compareTo 方法:按照字典序进行比较
与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。
比较方式:
·先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
·如果前k个字符相等(k为两个字符⻓度最⼩值),返回值两个字符串长度差值
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("abc");
String s4 = new String("abcdef");
System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1
System.out.println(s1.compareTo(s3)); // 相同输出0
System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出⻓度差值 -3
}(4)compareToIgnoreCase 方法:与compareTo方式相同,但是忽略大小写比较
String类提供的常用查找的方法:

public static void main(String[] args) {
String s = "aaabbbcccaaabbbccc";
System.out.println(s.charAt(3)); //'b'
System.out.println(s.indexOf('c')); //6
System.out.println(s.indexOf('c', 10)); //15
System.out.println(s.indexOf("bbb")); //3
System.out.println(s.indexOf("bbb", 10)); //2
System.out.println(s.lastIndexOf('c')); //17
System.out.println(s.lastIndexOf('c', 10)); //8
System.out.println(s.lastIndexOf("bbb")); //12
System.out.println(s.lastIndexOf("bbb", 10)); //3
}(1)数值和字符串转化
public static void main(String[] args) {
// 数字转字符串
String s1 = String.valueOf(1234);
String s2 = String.valueOf(12.34);
String s3 = String.valueOf(true);
String s4 = String.valueOf(new Student("Hanmeimei", 18));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println("=================================");
// 字符串转数字
// 注意:Integer、Double等是Java中的包装类型,这个后⾯会讲到
int data1 = Integer.parseInt("1234");
double data2 = Double.parseDouble("12.34");
System.out.println(data1);
System.out.println(data2);
}(2)大小写转换
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
// ⼩写转⼤写
System.out.println(s1.toUpperCase());
// ⼤写转⼩写
System.out.println(s2.toLowerCase());
}(3)字符串转数组
public static void main(String[] args) {
String s = "hello";
// 字符串转数组
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i]);
}
System.out.println();
// 数组转字符串
String s2 = new String(ch);
System.out.println(s2);
}(4)格式化
public static void main(String[] args) {
String s = String.format("%d-%d-%d", 2025, 6,9);
System.out.println(s);
}使用⼀个指定的新的字符串替换掉已有的字符串数据,可用的⽅法如下:

String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_"));
System.out.println(str.replaceFirst("l", "_"));#注:由于字符串是不可变对象,替换不修改当前字符串,而是产⽣⼀个新的字符串.
可用方法如下:

代码示例:实现字符串的拆分处理
String str = "hello world hello bit" ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {
System.out.println(s);
}代码示例:字符串的部分拆分
String str = "hello world hello bit" ;
String[] result = str.split(" ",2) ; //拆分成2段
for(String s: result) {
System.out.println(s);
}代码示例:拆分IP地址
String str = "192.168.1.1" ;
String[] result = str.split("\\.") ;
for(String s: result) {
System.out.println(s);
}#注:
(1)字符"|" , "*" , "+" ,“.” 都得加上转义字符,前面加上 "\\" .
(2)而如果是 "\" ,那么就得写成 "\\\\"
(3)如果⼀个字符串中有多个分隔符,可以⽤"|"作为连字符
可用方法如下:

String str = "helloworld" ;
System.out.println(str.substring(5));
System.out.println(str.substring(0, 5));#注:
(1)索引从0开始
(2)注意前闭后开区间的写法,substring(0,5)表示包含0号下标的字符,不包含5号下标

String str = " hello world " ;
System.out.println("["+str+"]");
System.out.println("["+str.trim()+"]");String是⼀种不可变对象. 字符串中的内容是不可改变。字符串不可被修改
1.StringBuilder和StringBuffer从方法的功能上看来说没有太大的区别。
public class Test {
public static void main(String[] args) {
long start = System.currentTimeMillis();
String s = "";
for(int i = 0; i < 10000; ++i){
s += i;
}
long end = System.currentTimeMillis();
System.out.println(end - start);
start = System.currentTimeMillis();
StringBuffer sbf = new StringBuffer("");
for(int i = 0; i < 10000; ++i){
sbf.append(i);
}
end = System.currentTimeMillis();
System.out.println(end - start);
start = System.currentTimeMillis();
StringBuilder sbd = new StringBuilder();
for(int i = 0; i < 10000; ++i){
sbd.append(i);
}
end = System.currentTimeMillis();
System.out.println(end - start);
}
}我们可以看到,同样的拼接10000次,String的效率非常慢,而StringBuilder和StringBuffer的效率却非常快,这是因为String在拼接时会不断的进行产生新的对象,而StringBuilder和StringBuffer则是直接用当前的对象进行拼接,这就导致了String的效率慢,,因此:尽量避免对String的直接需要,如果要修改建议尽量使用StringBuffer或者StringBuilder。

public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder("hello");
StringBuilder sb2 = sb1;
//追加:即尾插-->字符、字符串、整形数字
sb1.append(' '); // hello
sb1.append("world"); // hello world
sb1.append(123); // hello world123
System.out.println(sb1); // hello world123
System.out.println(sb1 == sb2); // true
System.out.println(sb1.charAt(0)); //获取0号位上的字符h
System.out.println(sb1.length()); //获取字符串的有效⻓度14
System.out.println(sb1.capacity());//获取底层数组的总⼤⼩
sb1.setCharAt(0, 'H'); //设置任意位置的字符Hello world123
sb1.insert(0, "Hello world!!!");// Hello world!!!Hello world123
System.out.println(sb1);
System.out.println(sb1.indexOf("Hello")); //获取Hello第⼀次出现的位置
System.out.println(sb1.lastIndexOf("hello")); //获取 hello 最后⼀次出现的位置
sb1.deleteCharAt(0); //删除⾸字符
sb1.delete(0,5); //删除[0, 5)范围内的字符
String str = sb1.substring(0, 5); //截取[0, 5)区间中的字符以 String 的⽅式返回
System.out.println(str);
sb1.reverse(); //字符串逆转
str = sb1.toString(); //将StringBuffer以String的⽅式返回
System.out.println(str);
}从上述例⼦可以看出:String和StringBuilder最大的区别在于String的内容无法修改,而 StringBuilder的内容可以修改。频繁修改字符串的情况考虑使用StringBuilder。
#注:String和StringBuilder类不能直接转换。如果要想互相转换,可以采⽤如下原则:
• String变为StringBuilder: 利用StringBuilder的构造方法或append()方法
• StringBuilder变为String: 调用toString()方法。


StringBuffer采用同步处理,属于线程安全操作,常用于多线程情况下,可以保证线程安全;而StringBuilder未采用同步处理,属于线程不安全操作,常用于单线程情况下