-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringMethodTest.java
More file actions
68 lines (51 loc) · 1.71 KB
/
StringMethodTest.java
File metadata and controls
68 lines (51 loc) · 1.71 KB
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 String;
import org.junit.Test;
public class StringMethodTest {
@Test
public void test4(){
String str1 = "helloworld";
System.out.println(str1.replace('h', 'H'));
}
@Test
public void test3(){
String str1 = "helloworld";
System.out.println(str1.endsWith("ld"));
System.out.println(str1.startsWith("hh"));
//记录指定字符串的内容以及开始的位置
System.out.println(str1.startsWith("ll", 2));
System.out.println(str1.contains("wo"));
//返回字符串的角标,如果没有则返回-1
System.out.println(str1.indexOf("lo"));
System.out.println(str1.lastIndexOf("or"));
}
@Test
public void test2(){
String s1 = "HelloWorld";
String s2 = "helloworld";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
String s3 = "abc";
String s4 = s3.concat("def");
System.out.println(s4);
String s5 = "abc";
String s6 = "abe";
//逐个比较两个字符串,返回值数据类型为int
System.out.println(s5.compareTo(s6));//用于通讯录排序
String s7 = "helloworld";
System.out.println(s7.substring(3));
System.out.println(s7.substring(2, 4));
}
@Test
public void test1(){
String s1 = "HelloWorld";
System.out.println(s1.length());
System.out.println(s1.charAt(0));
System.out.println(s1.isEmpty());
String s2 = s1.toLowerCase();
System.out.println(s2);
String s3 = " hello w orld ";
//去除首尾的空格
String s4 = s3.trim();
System.out.println(s4);
}
}