1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package _22javacoreclass;

public class Main{
public static void main(String[] args){
String s1 = "hello";
String s2 = "HELLO";
String s3 = new String(new char[]{'h','e','l','l','o'});
String s4 = s1.toUpperCase();


// 1.字符串比较
System.out.println(s1.equals(s4)); // false
// 2.忽略大小写比较
System.out.println(s1.equalsIgnoreCase(s2)); // true
// 3.是否包含子串
System.out.println("Hello".contains("ll")); // true
// 4.返回第一个匹配字符的索引
System.out.println("Hello".indexOf("l")); // 2
// 5.返回最后一个匹配字符的索引
System.out.println("Hello".lastIndexOf("l")); // 3
// 6.是否以指定字符串前缀开始
System.out.println("Hello".startsWith("He")); // true
// 7.是否以指定字符串结尾
System.out.println("Hello".endsWith("lo")); // true
// 8.提取2到结尾子串
System.out.println("Hello".substring(2)); //"llo"
// 9.提取2到3子串
System.out.println("Hello".substring(2, 4)); //"ll"
// 10.去除收尾空白字符
System.out.println(" \tHello\r\n ".trim()); // "Hello"
// 11.去空格 java11以后特性
// System.out.println("\u3000Hello\u3000".strip()); // "Hello"
// 12.去前缀空格 java11以后特性
// System.out.println(" Hello ".stripLeading()); // "Hello "
// 13.去结尾空格 java11以后特性
// System.out.println(" Hello ".stripTrailing()); // " Hello"
// 14.判断字符串是否为空
System.out.println("".isEmpty()); //true
System.out.println(" ".isEmpty()); //false
// 15.判断字符串是否包含空白字符串 java11以后特性
// System.out.println(" \n".isBlank()); //true
// System.out.println(" Hello ".isBlank()); //false
// 16.替换子串
System.out.println("hello".replace('l','w')); // hewwo
// 17.表达式替换
System.out.println("A,,B;C ,D".replaceAll("[\\,\\;\\s]+", ",")); // A,B,C,D
// 18.分割字符串
System.out.println("A,B,C,D".split("\\,")); // {"A","B","C","D"}
// 19.拼接字符串
String[] arr = {"A", "B", "C"};
System.out.println(String.join("***",arr)); //A***B***C
// 20.类型转换为字符串
System.out.println(String.valueOf(123)); // "123"
System.out.println(String.valueOf(45.67)); // "45.67"
System.out.println(String.valueOf(true)); // "true"
System.out.println(String.valueOf(new Object())); //类似java.lang.Object@4554617c
// 21.字符串转换为int
System.out.println(Integer.parseInt("123")); //123
System.out.println(Integer.parseInt("ff",16)); //按十六进制转换, 255
// 22.字符串转换为boolean
System.out.println(Boolean.parseBoolean("true")); //true
System.out.println(Boolean.parseBoolean("FASLE")); //false
// 23.转换为char[]
char[] cs = "Hello".toCharArray();
System.out.println(cs); //String转char[], Hello
System.out.println(new String(cs)); //char[]转String, Hello
}
}