-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestString1.java
70 lines (58 loc) · 1.61 KB
/
TestString1.java
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
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestString1 {
private static String str = "this is a test of java";
public static void main(String[] args) {
// TODO ×Ô¶¯Éú³ÉµÄ·½·¨´æ¸ù
test1();
test2();
test3();
test4();
test5();
test6();
}
public static void test1(){
int count = 0;
for (int i = 0; i < str.length(); i++) {
if('s'==str.charAt(i)){
count++;
}
}
System.out.println(count);
}
public static void test2(){
int start = str.indexOf("test");
String s = str.substring(start,start+5);
System.out.println(s);
}
public static void test3(){
char[] ch2 = str.toCharArray();
System.out.println(Arrays.toString(ch2));
}
public static void test4(){
StringBuilder sb = new StringBuilder();
Pattern pattern = Pattern.compile("^[a-zA-Z]+|\\S[a-zA-Z]*");
Matcher matcher = pattern.matcher(str);
String tempString = null;
while(matcher.find()) {
tempString = matcher.group();
tempString = (tempString.charAt(0)+"").toUpperCase()+tempString.substring(1,tempString.length());
sb.append(tempString).append(" ");
}
System.out.println(sb.toString());
}
public static void test5(){
StringBuffer sb = new StringBuffer(str);
System.out.println(sb.reverse().toString());
StringBuffer sb2 = new StringBuffer();
for (int i = str.length()-1; i >=0; i--) {
sb2.append(str.charAt(i));
}
System.out.println(sb2);
}
public static void test6() {
String[] arr = str.split(" ");
System.out.println(Arrays.toString(arr));
}
}