该文章方法预览:

1. 常用方法
1.1 字符串构造
常用的三种构造方式:
public static void main(String[] args) {
// 使用常量串构造
String s1 = "hello";
// 直接 newString 对象
String s2 = new String("world");
// 使用字符数组进行构造
char[] array = {'l','o','v','e'};
String s3 = new String(array);
}
tips:
1. String 是引用类型,内部不存储字符串本身,如下代码:
public static void main(String[] args) { String s1 = new String("hello"); String s2 = new String("world"); String s3 = s1; }调试后发现 s1 中有四个变量,我们只需关心其中 value 的值
实例化演示图如下:
s1 和 s2 引用的是不同对象,s1 和 s3 引用的是同一对象
2. 在 Java 中,双引号 " " 引起来的也是 String 类型,如下:
// 打印 "hello" 字符串的长度 System.out.println("hello".length());
1.2 String 对象的比较
1. == 比较是否引用了同一个对象
tip:对于内置类型,== 比较的是变量中的值;对于引用类型,== 比较的是引用中的地址
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("world");
String s4 = s1;
System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.println(s1 == s4);
}
运行结果:

2. boolean equals(Object anObject) 方法:按照字典序比较
字典序:字符大小的顺序
String 类重写了父类 Object 中的 equals 方法,Object 中 equals 默认按照 == 比较,String 重写 equals 方法后,按照如下规则进行比较:
public boolean equals(Object anObject) {
// 1. 先检测 this 和 anObject 是否为同一个对象比较,如果是返回 true
if (this == anObject) {
return true;
}
// 2. 检测 anObject 是否为 String 类型的对象,如果是继续比较,否则返回 false
if (anObject instanceof String) {
// 将 anObject 向下转型为 String 类型对象
String anotherString = (String) anObject;
int n = value.length;
// 3. this 和 anObject 两个字符串的长度是否相同,是继续比较,否则返回 false
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
// 4. 按照字典序,从前往后逐个字符进行比较
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("Hello");
// 虽然 s1 与 s2 引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出 true
System.out.println(s1.equals(s2));
// s1 与 s3 引用的不是同一个对象,且两个对象中放置内容也不同,因此输出 false
System.out.println(s1.equals(s3));
}
3. int compareTo(String s) 方法:按照字典序进行比较
与 equals 方法不同的是,equals 返回的是 boolean 类型,而 compareTo 返回的是 int 类型
具体比较方式:
1、先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
2、如果前 n 个字符相等(n 为两个字符串中长度最小值),返回这两个字符串的长度差值
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)); // 前 n 个字符完全相同,输出长度差值 -3
}
4. int compareTolgnoreCase(String s) 与 compareTo 方式相同,但是忽略大小写比较
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.compareToIgnoreCase(s2)); // 不同输出字符差值 -1
System.out.println(s1.compareToIgnoreCase(s3)); // 忽略大小写,相同输出 0
System.out.println(s1.compareToIgnoreCase(s4)); // 前 n 个字符完全相同,输出长度差值 -3
}
1.3 字符串查找

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)); // 12
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.4 转化
1. 数值和字符串转化
public static void main(String[] args) {
// 数字转字符串
String s1 = String.valueOf(1234);
String s2 = String.valueOf(3.14);
String s3 = String.valueOf(true);
String s4 = String.valueOf(new Student("zhangsan",18));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println("===========");
// 字符串转数字
int data1 = Integer.parseInt("1234");
double data2 = Double.parseDouble("3.14");
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());
}
tip:转换的字符串中不能含有除字母外其他符号
3. 字符串转数组
public static void main(String[] args) {
String s1 = "hello";
// 字符串转数组
char[] ch = s1.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",2024,5,20);
System.out.println(s); // 运行结果:2024-5-20
}
1.5 字符串替换
使用一个指定的新字符串替换掉已有的字符串数据,可用方法如下:

示例:
public static void main(String[] args) {
String str = "helloworld";
System.out.println(str.replaceAll("l","="));
System.out.println(str.replaceFirst("l","="));
}
运行结果:

tips:由于字符串是不可变对象,替换不修改当前字符串,而是产生一个新的字符串
1.6 字符串拆分
可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串,可用方法如下:

示例:
字符串全部拆分:
public static void main(String[] args) {
String str = "hello world hello haha";
String[] result = str.split(" "); // 按照空格拆分
for(String s : result) {
System.out.println(s);
}
}
/*
运行结果:
hello
world
hello
haha
*/
字符串部分拆分:
public static void main(String[] args) {
String str = "hello world hello haha";
String[] result = str.split(" ",2); // 按照空格拆分为两部分
for(String s : result) {
System.out.println(s);
}
}
/*
运行结果:
hello
world hello haha
*/
tip:有些特殊字符作为分隔符可能无法正确拆分,需要加上转义,如拆分 IP 地址:
public static void main(String[] args) {
String str = "192.168.1.1";
String[] result = str.split("\\."); // 需加转义字符
for(String s : result) {
System.out.println(s);
}
}
tips:
- 字符 "|" "*" "+" 前都需加转义字符 "\\"
- 如果是 "\" 那就要写成 "\\\\"
- 如果一个字符串中有多个分隔符,可以用 "|" 作为连字符
示例:多次拆分
public static void main(String[] args) {
String str = "name=zhangsan&age=18";
String[] result = str.split("&");
for (int i = 0; i < result.length; i++) {
String[] temp = result[i].split("=");
System.out.println(temp[0] + " = " + temp[1]);
}
}
/*
运行结果:
name = zhangsan
age = 18
*/
1.7 字符串截取
从一个完整的字符串中截取部分内容,可用方法如下:

示例:
public static void main(String[] args) {
String str = "helloworld";
System.out.println(str.substring(5)); // 从下标位置截取到结尾
System.out.println(str.substring(0,5)); // 从 0 下标位置截取到下标 5 之前,不包含 5 坐标
}
/*
运行结果:
world
hello
*/
tip:Java 中大部分带有区间的规则都是左闭右开
1.8 其他操作方法

示例:
trim() 方法会去掉字符串开头和结尾的空白字符(空格、换行符、制表符等)
public static void main(String[] args) {
String str = " hello world ";
System.out.println("["+str+"]");
System.out.println("["+str.trim()+"]");
}
/*
运行结果:
[ hello world ]
[hello world]
*/
大小写转换:
public static void main(String[] args) {
String str = " hello%$$%@#$%world 哈哈哈 ";
System.out.println(str.toUpperCase());
System.out.println(str.toLowerCase());
}
/*
tip:这两个函数只转换字母
运行结果:
HELLO%$$%@#$%WORLD 哈哈哈
hello%$$%@#$%world 哈哈哈
*/
1.9 字符串的不可变性
String 是一种不可改变的对象,字符串中的内容是不可改变的,原因如下:
我们看 String 的源码:

我们可以看出 String 类中的字符实际保存在内部维护的 value 字符数组中
其中第一个 final 表明该类不可被继承,不是 String 不可改变的原因
第二个 final 表明 value 自身的值不能改变,即不能引用其它字符数组,但是其引用空间中的内容可以修改,不是 String 不可改变的原因
String 不可改变的真正原因是因为第三点,它由 private 所修饰,只能在当前类中使用,且并未提供任何 get、set 方法,因此无法在类外任何地方改变字符串内容
因此只要涉及到 String 类型的转变,都不是在原有的字符串上进行的修改,而是会产生一个新对象
1.10 字符串修改
tip:尽量避免直接对 String 类型对象进行修改,因为 String 类不能修改,所有的修改都会创建新对象,效率非常低,示例如下:
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);
}
/*
运行结果:
85
0
0
*/
通过运行结果可以看出在对 String 类进行修改时,效率是非常慢的,因此要尽量避免对 String 的直接修改,如果要修改建议尽量使用 StringBuffer 和 StringBuilder(Java——StringBuffer和StringBuilder详解)





2822

被折叠的 条评论
为什么被折叠?



