目录
②boolean equals(Object anObject) 方法 : 按照字典序比较
③int compareTo(String s) 方法 : 按照字典序进行比较
④int compareToIgnoreCase(String str) 方法 : 与compareTo() 方式相同, 但是忽略大小写
一.Java 中的String类
Java 的 String 类是 java.lang 包中的一个核心类,用于表示和操作字符串。字符串在 Java 中是不可变的(immutable),任何对字符串的修改操作都会返回一个新的字符串对象。
二.常用方法
1.字符串构造 (3种)
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hellw");
char[] ch = {'h','e','l','l','a'};
String s3 = new String(ch);
}
①使用常量串构造

②直接 newString 对象
![]()
③使用字符数组 进行 构造

注意:
String是引用类型,内部并不存储字符串本身
在Java中 用双引号引起来的也是 String类型对象
2.String 对象的比较(4种)
① == 比较是否引用同一个对象
对于基本类型,比较的是变量中的值
对于引用类型,比较的是引用中的地址
public static void main(String[] args) {
//基本类型变量,比较变量中存储的值是否相等
int a = 10;
int b = 20;
int c = 10;
System.out.println(a == c);//true
System.out.println(b == c);//false
//引用类型变量,比较两个引用变量引用的是否是同一个对象
String s1 = new String("hello");
String s2 = new String("world");
String s3 = new String("hello");
String s4 = s1;
System.out.println(s1 == s2);//false
System.out.println(s1 == s3);//false
System.out.println(s1 == s4);//true
}
②boolean equals(Object anObject) 方法 : 按照字典序比较
String 类重写了 父类Object 类 的 equals() 方法
Object 类 的 equals() 方法默认按照 == 比较
String重写equals方法后, 按照:s1.equals(s2)
s1.equals(s2): 比较String的是 逐个字符
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("world");
String s3 = new String("hello");
String s4 = s1;
System.out.println(s1.equals(s2));//false
System.out.println(s1.equals(s3));//true
System.out.println(s1.equals(s4));//true
}
③int compareTo(String s) 方法 : 按照字典序进行比较
与equals不同的是, equals 返回的是 boolean 类型, 而compareTo 返回的int类型
public static void main(String[] args) {
String s1 = new String("abcde");
String s2 = new String("abc");
String s3 = new String("ABC");
String s4 = new String("abcde");
System.out.println(s1.compareTo(s2));//2 前k个字符串相等,返回长度差值2
System.out.println(s1.compareTo(s3));//32 返回符号差值32
System.out.println(s1.compareTo(s4));//0 相等返回0
}
- 先按照字典序大小比较, 如果出现不等的字符串, 直接返回 这两个字符的大小差值
- 如果前k个字符相等 (k为长度较小的字符串的长度) , 返回值为 两个字符串长度的差值 ; 即对应字符无法比较出大小 , 返回长度差
④int compareToIgnoreCase(String str) 方法 : 与compareTo() 方式相同, 但是忽略大小写
public static void main(String[] args) {
String s1 = new String("abcde");
String s2 = new String("abc");
String s3 = new String("ABC");
String s4 = new String("abcde");
System.out.println(s1.compareToIgnoreCase(s2));//2 前k个字符串相等,返回长度差值2
System.out.println(s1.compareToIgnoreCase(s3));//2 忽略大小写后,前k个字符串相等,返回长度差值2
System.out.println(s1.compareToIgnoreCase(s4));//0 相等返回0
}
3.字符串查找
public static void main(String[] args) {
String s1 = new String("aaabbbcccaaabbbccc");
System.out.println(s1.charAt(3));//b 返回3下标的字符'b'
System.out.println(s1.indexOf("b"));//3 返回b(可以是字符或数字)第一次出现的下标,没有则返回-1
System.out.println(s1.indexOf("b",2));//3 返回b(可以是字符或数字)从下标2开始,第一次出现的下标,没有则返回-1
System.out.println(s1.indexOf("bbb",2));//3
System.out.println(s1.lastIndexOf("b"));//14 从后往前找,返回b(可以是字符或数字)第一次出现的下标,没有则返回-1
System.out.println(s1.lastIndexOf("b",10));//5 从10下标开始,从后往前找,返回b(可以是字符或数字)第一次出现的下标,没有则返回-1
System.out.println(s1.lastIndexOf("bbb",10));//3
}

4.转化
①数值和字符串转化
Ⅰ 数字转字符串:
class Student{
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public static void main(String[] args) {
String s1 = String.valueOf(1234);
String s2 = String.valueOf(true);
String s3 = String.valueOf(new Student("zhangsan",22));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}

Ⅱ 字符串转数字:
Integer , Double等是Java的包装类
public static void main(String[] args) {
int d1 = Integer.parseInt("1234");
double d2 = Double.parseDouble("23.33");
System.out.println(d1);
System.out.println(d2);
}

②大小写转换
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
System.out.println(s1.toUpperCase());//将小写转为大写
System.out.println(s2.toLowerCase());//将大写转为小写
}

③字符串转数组
Ⅰ 字符串转数组
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]);
}
}
![]()
Ⅱ 数组转字符串
public static void main(String[] args) {
char[] ch = {'h','e','l','l','o'};
String s1 = new String(ch);
System.out.println(s1);
}
![]()
④格式化
public static void main(String[] args) {
String s = String.format("%d / %d / %d",2025,8,1);
System.out.println(s);
}
![]()
5.字符串替换
public static void main(String[] args) {
String s1 = new String("hello");
System.out.println(s1.replace("o","ee"));//用ee(可以是字符或字符串) 替换o(可以是字符或者是字符串)
System.out.println(s1.replaceAll("l","-"));//用- 替换所有l
System.out.println(s1.replaceFirst("l","-"));//用- 替换第一个l
}

| 方法 | 功能 |
| replace(char olderChar,char newChar) | 替换所有指定内容 |
| replace(CharSepuence target,CharSequence replancement) | 替换所有指定内容(参数可以是字符串) |
| String replaceAll(String regex,String replacement) | 替换所有指定内容(第一个参数是正则表达式) |
| String replaceFirst(String regex,String replacement) | 替换第一次出现的指定内容 |
| 第一个参数是原内容,第二个是替换内容 |
6.字符串拆分
将完整的字符串按照指定的分隔符划分为若干子字符串
①字符串的部分拆分
public static void main(String[] args) {
String s1 = "hello world";
String [] s2 = s1.split(" ");//用空格作为分隔符
for (String s:s2) {
System.out.println(s);
}
}
![]()
②带有转义字符的拆分
public static void main(String[] args) {
String s1 = "192.163.1.1";
String [] s2 = s1.split("\\.");// \\.表示.
for (String s:s2) {
System.out.println(s);
}
}
![]()
注意:
- 字符 | * + . 等都要在前面加上转义字符 \\
- 如果是分隔符是 \, 则需要写成 \\\\, 两个斜杠代表一个斜杠
- 如果一i个字符串有多个分隔符, 则需要用 | 作为连字符
③多次拆分
public static void main(String[] args) {
String s1 = "name=zhangsan&age=10";
String[] s2 = s1.split("&");
for (int i = 0; i < s2.length; i++) {
String[] s3 = s2[i].split("=");
for (int j = 0; j < s3.length; j++) {
System.out.print(s3[j]);
}
}
}

7.字符串截取
public static void main(String[] args) {
String s = "helloworld";
System.out.println(s.substring(5));//从指定位置截取到末尾
System.out.println(s.substring(0,5));//从开始位置(参数一)截取到(参数二),范围是前闭后开区间
}
![]()

8.其他方法
public static void main(String[] args) {
String s1 = " hello world ";
System.out.println("{"+s1.trim()+"}");//只是去除字符串 最左侧和左右侧 的空格
}
trim() 只会去除字符串 开头和结尾 的空白字符串(空格, 换行, 制表符)

9.字符串的不可变性
String是一种不可变对象.字符串的内容是不可改变的
①String 类在设计时就是不可变的, String类实现描述已经说明了:String类中的字符实际保存在内部维护的value字符数组中
- String类被final修饰, 表明该类不能被继承
- value被final修饰, 表明value自身的值不能被更改, 即不能引用其它字符数组, 但是其应用空间中的内容可以修改
②所有涉及到可能修改字符串内容的操作都是创建一个新的对象那个, 改变的是新对象
final修饰类表明该类不想被继承, final修饰应用类型表明该引用变量不能引用其他对象, 但是其引用对象中的内容是可以修改的

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



