一、String类的重要性
在C语言中已经涉及到字符串了,但是在C语言中要表示字符串只能使用字符数组或者字符指针,可以使用标准库提 供的字符串系列函数完成大部分操作,但是这种将数据和操作数据方法分离开的方式不符合面相对象的思想,而字 符串应用又非常广泛,因此Java语言专门提供了String类
二、String类的构造方式
String类提供的构造方式非常多,常用的就以下三种:
public static void main(String[] args) {
// 使用常量串构造
String s1 = "hello bit";
System.out.println(s1);
// 直接newString对象
String s2 = new String("hello bit");
System.out.println(s1);
// 使用字符数组进行构造
char[] array = {'h','e','l','l','o','b','i','t'};
String s3 = new String(array);
System.out.println(s1);
}
想了解更多关于String类的构造方式,请点击链接:https://docs.oracle.com/javase/8/docs/api/index.html
注意:
1. String是引用类型,内部并不存储字符串本身
public static void main(String[] args) { // s1和s2引用的是不同对象 s1和s3引用的是同一对象 String s1 = new String("hello"); String s2 = new String("world"); String s3 = s1; System.out.println(s1.length()); // 获取字符串长度---输出5 System.out.println(s1.isEmpty()); // 如果字符串长度为0,返回true,否则返回false }
2. 在Java中 " " 引起来的也是String类型对象
// 打印"hello"字符串(String对象)的长度
System.out.println("hello".length());
3. String对象的内容不可改变, 被称为不可变字符串对象
4. 所有涉及到可能修改字符串内容的操作都是创建一个新对象,改变的是新对象
比如:String类中的replace方法

5. 只要是以 "…" 方式写出的字符串对象, 会在堆内存中的字符串常量池中存储,且相同内 容只存储一份,但通过new 方式创建字符串,每new 一次都会产生一个新的对象放在堆内 存中
class Test1 { public static void main(String[] args) { String s1 = "abc"; String s2 = "abc"; System.out.println(s1 == s2); //true } }
class Test2 { public static void main(String[] args) { char[] chars = {'a', 'b', 'c'}; String s1 = new String(chars); String s2 = new String(chars); System.out.println(s1 == s2); //false } }
class Test3 { public static void main(String[] args) { String s1 = new String("abc"); // 这行代码创建了2个对象 String s2 = "abc"; // 这行代码创建了0个对象 System.out.println(s1 == s2); // false } }
三、String的常用方法
3.1 String对象的比较
字符串的比较是常见操作之一,Java中总共提供了4种比较方式:
3.1.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
}
3.1.2 boolean equals(Object anObject)
方法:按照字典序比较(字典序:字符大小的顺序 )
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("Hello");
// equals比较:String对象中的逐个字符
// 虽然s1与s2引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出true
// s1与s3引用的不是同一个对象,而且两个对象中内容也不同,因此输出false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false
}
3.1.3 int compareTo(String s)
方法: 按照字典序进行比较
与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型
原理:
1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
2. 如果前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
}
3.1.4 int compareToIgnoreCase(String str)
方法:与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)); // 前k个字符完全相同,输出长度差值-3
}
3.2 字符串查找
字符串查找是非常常见的操作,String类提供了很多方法,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)); // 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
}
3.3 转换
3.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);
// 字符串转数字
int data1 = Integer.parseInt("1234");
double data2 = Double.parseDouble("12.34");
System.out.println(data1);
System.out.println(data2);
}
3.3.2 大小写转换
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
// 小写转大写
System.out.println(s1.toUpperCase());
// 大写转小写
System.out.println(s2.toLowerCase());
}
3.3.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]);
}
// 数组转字符串
String s2 = new String(ch);
System.out.println(s2);
}
3.3.4 格式化
public static void main(String[] args) {
String s = String.format("%d-%d-%d", 2019, 9,14); // 2019-9-14
System.out.println(s);
}
四、字符串替换
使用一个指定的新的字符串替换掉已有的字符串数据

示例:
public static void main(String[] args) {
String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_")); // he__owor_d
System.out.println(str.replaceFirst("l", "_")); // he_loworld
}
注意:由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串
五、字符串拆分
可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串

// 实现字符串的拆分处理 String str = "hello world hello sir" ; String[] result1 = str.split(" ") ; // 按照空格拆分 for(String s: result1) { System.out.println(s); } // 字符串的部分拆分 String[] result2 = str.split(" ",2) ; for(String s: result2) { System.out.println(s); }
有些特殊字符作为分割符可能无法正确切分, 需要加上转义
String str = "192.168.1.1" ;
String[] result = str.split("\\.") ;
for(String s: result) {
System.out.println(s);
}
注意:
1. 字符" | ", " * " ," + "都得加上转义字符,前面加上 " \\ "
2. 而如果是 " \ " ,那么就得写成 " \\\\ "
3. 如果一个字符串中有多个分隔符,可以用" | "作为连字符
// 需要拆分多个分隔符时,使用" | "
String str = "name=zhangsan&age=18" ;
String[] result = str.split("=|&");
for(String x : result) {
System.out.println(x);
}
六、字符串截取
从一个完整的字符串之中截取出部分内容

String str = "helloworld" ; System.out.println(str.substring(5)); // world System.out.println(str.substring(0, 5)); // hello
注意:
1. 索引从0开始
2. 注意前闭后开区间的写法, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标
七、大小写转换和 trim() 方法

// trim()方法的使用
String str = " hello world " ;
System.out.println("["+str+"]"); // [ hello world ]
System.out.println("["+str.trim()+"]"); // [hello world]
注意:trim 会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等)
// 大小写转换 String str = " hello%$$%@#$%world 哈哈哈 " ; System.out.println(str.toUpperCase()); // HELLO%$$%@#$%WORLD 哈哈哈 System.out.println(str.toLowerCase()); // hello%$$%@#$%world 哈哈哈
注意:toUpperCase 和 toLowerCase 只转换字母





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



