-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStringTest.java
More file actions
84 lines (62 loc) · 1.97 KB
/
StringTest.java
File metadata and controls
84 lines (62 loc) · 1.97 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package lang;
import static unit.Unit.assertEquals;
import java.lang.reflect.Field;
import java.util.Arrays;
public class StringTest {
public static void main(String[] args) {
test();
test_reflection();
}
// 改变字符串
private static void test_reflection() {
String test = "test";
try {
Field filed = String.class.getDeclaredField("value");
filed.setAccessible(true);
char[] val = (char[]) filed.get(test);
val[0] = 'T';
assertEquals("Test", test);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
public static void test() {
int length = "test".length();
assertEquals(4, length);
boolean bc = "test".contains("tes");
assertEquals(true, bc);
boolean bs = "test".startsWith("tes");
assertEquals(true, bs);
boolean be = "test".endsWith("st");
assertEquals(true, be);
char cc = "test".charAt(3);
assertEquals('t', cc);
int ii = "test".indexOf('e');
assertEquals(1, ii);
String trim = " test ".trim();
assertEquals("test", trim);
String[] es = "test".split("e");
assertEquals(2, es.length);
assertEquals("t", es[0]);
assertEquals("st", es[1]);
String substring = "test".substring(1, 2);
assertEquals("e", substring);
String uc = "test".toUpperCase();
assertEquals("TEST", uc);
String lc = "tEst".toLowerCase();
assertEquals("test", lc);
String replace = "test".replace("te", "T");
assertEquals("Tst", replace);
String ra = "stest".replaceAll(".{2}t", "T");
assertEquals("stT", ra);
String x = "test";
char[] chars = x.toCharArray();
chars[1] = 'E';
char xc = x.charAt(1);
assertEquals('e', xc); // 说明 x.toCharArray 的返回值是成员变量 value 的副本。
String test = String.join(",", "x", "y", "z");
assertEquals("x,y,z", test);
String j2 = String.join(",", Arrays.asList("x", "y", "z"));
assertEquals("x,y,z", j2);
}
}