-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringMethodTest.java
More file actions
200 lines (146 loc) · 5.1 KB
/
Copy pathStringMethodTest.java
File metadata and controls
200 lines (146 loc) · 5.1 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.alexpower.java;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/*
* length()
* charAt()
* isEmpty()
* toLowerCase()
* toUpperCase()
* trim()
*
* equals
* equalsIngnoreCase()
*
* startsWith()
* endsWith()
*
* indexOf() return the index of first char, or -1 if no result
*
*
*
*
*
* */
public class StringMethodTest {
@Test
public void test1(){
String s1 = "hellowrold";
System.out.println(s1.length());
System.out.println(s1.charAt(9));
System.out.println(s1.charAt(0));
System.out.println(s1.charAt(2));
// System.out.println(s1.charAt(10)); // error
// s1 = "";
System.out.println(s1.isEmpty()); // true
// tolower & upper case, create new string, does not alter s1,
String s2 = s1.toUpperCase();
System.out.println(s2); // HELLOWORLD
System.out.println(s1); // helloworld
// trim(), remove the both ends space, used when user insert the username or number
String s3= " hel lo wo rld ";
String s4 = s3.trim();
System.out.println("---" + s3 + "----");
System.out.println("---" + s4 + "----");
// equals just used for comparing the content, not the reference.
// equalsIgnoreCase() , ignore the case
}
@Test
public void test2(){
String s1 = "HelloWrold";
String s2 = "hellowrold";
System.out.println(s1.equals(s2)); // false
System.out.println(s1.equalsIgnoreCase(s2)); // true
// concat()
String s3 = "today is ";
String s4 = s3.concat("an amazing day!");
System.out.println(s4);
// compareTo()
String s5 = "abc";
String s6= "abe";
String s7 = "zbe";
System.out.println(s5.compareTo(s6)); // -2
System.out.println(s5.compareTo(s7));
// subString(beginIndex, (endIndex));
String s8 = "中国陕西西安";
String s9 = s8.substring(2); // begin index : 陕西西安
System.out.println("---s8: " + s8+ "---s9: " + s9);
s9 = s8.substring(4,6); // begin index, end index
System.out.println(s9);
}
@Test
public void test3(){
String s1 = "helloworld";
System.out.println(s1.endsWith("d")); // true
System.out.println(s1.startsWith("He")); // true
System.out.println(s1.startsWith("ll",2)); // true, startfrom index 2 is "ll"
String str2 = "wo";
System.out.println(s1.contains(str2)); // true
System.out.println(s1.indexOf("lo")); // 3, index of the first char,
// returns -1 if no result
System.out.println(s1.indexOf("lo", 5)); // -1
String str3 = "hellorworld";
System.out.println(str3.lastIndexOf("or")); // 7, the last "or"
System.out.println(str3.lastIndexOf("or", 6)); // 4, the first "or"
}
@Test
public void test4(){
// String && int
String str = "23";
Integer int1 = Integer.parseInt(str);
System.out.println(int1);
int x = 100;
String str2 = String.valueOf(x);
System.out.println(str2);
}
@Test
public void test5(){
// String && char[]
String str1= "abcqwe";
char[] charArray = str1.toCharArray();
for(int x= 0; x < charArray.length; x++){
System.out.println(charArray[x]);
}
char[] charArray2 = new char[]{'1', '2','3'};
String str2 = new String(charArray2);
System.out.println(str2);
// challenge: make "abc123" to "a21cb3"
System.out.println("*******************");
String str3 = "abc123";
// char[] newCharArr = str3.toCharArray();
// System.out.println(newCharArr);
//
// System.out.println(str3.startsWith("b", 3));
System.out.println(str3.replace("bc12", "12cb"));
char[] newchar= new char[]{'1','2','3'};
// Collection.reverse(Arrays.asList(newchar));
}
@Test
public void test6() throws UnsupportedEncodingException {
// String and byte
// 编码: 字符串--> 字节(二进制) (看得懂-》 看不懂的二进制)
// 解码: 编码的逆过程: 字节 --> 字符串
// 编码
String str1 = "ABC中国";
byte[] bytes = str1.getBytes();
System.out.println(Arrays.toString(bytes)); // [65,66,67]
// byte[] bytes1 = str1.getBytes();
// String s = new String(bytes1);
// 解码
System.out.println(new String(bytes));
// gbk(编码系统) 编码 && 解码
byte[] gbks = str1.getBytes("gbk");
System.out.println(new String(gbks)); // 乱码
// 在解码时, 解码集必须与编码集一致, 否则会出现乱码
System.out.println(new String(gbks, "gbk")); // 解码正确
}
@Test
public void test7() {
String str = "1234567";
System.out.println(str.matches("\\d+")); // true
// tell if it's phone from Toronto, Canada
String phoneNum = "647-5507980";
System.out.println(phoneNum.matches("(647)-\\d{7,8}")); // true
}
}