forked from eaglesky/JavaPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestString.java
More file actions
150 lines (130 loc) · 4.8 KB
/
TestString.java
File metadata and controls
150 lines (130 loc) · 4.8 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
import java.util.*;
public class TestString {
private static void stringDemo() {
System.out.println("String demo");
String palindrome = "Dot saw I was Tod";
int len = palindrome.length();
char[] tempCharArray = new char[len];
char[] charArray = new char[len];
System.out.println("Original palindrome: ");
System.out.println(palindrome);
System.out.println("Length = " + len);
// put original string in an
// array of chars
System.out.print("Characters in palindrome :");
for (int i = 0; i < len; i++) {
tempCharArray[i] = palindrome.charAt(i);
System.out.print(tempCharArray[i] + " ");
}
System.out.println("");
// reverse array of chars
for (int j = 0; j < len; j++) {
charArray[j] = tempCharArray[len - 1 - j];
}
String reversePalindrome =
new String(charArray); //Wrapping a deep copy of the original charArray
System.out.println(reversePalindrome);
}
private static void stringBuilderDemo() {
System.out.println("String builder demo");
String palindrome = "Dot saw I was Tod";
StringBuilder sb = new StringBuilder(palindrome);
sb.reverse(); // reverse it
System.out.println(sb);
String reversed = sb.toString();
// The same as above, since println(sb) implicitly calls sb.toString()
System.out.println(reversed);
String[] strs = {"I ", "Love ", "Java"};
StringBuilder sb2 = new StringBuilder();
for (String str : strs) {
sb2.append(str);
}
String result = sb2.toString();
System.out.println(result);
sb2.setCharAt(2, 'l');
sb2.setCharAt(7, 'j');
System.out.println(sb2);
}
// StringBuilder is essentially a wrapper of char[],
// with some additional funtionalities including resizing the char array.
// If char array can do the job then we should just use char array
private static void modifyString(String str) {
char[] chars = str.toCharArray(); //Returns a deep copy of the char array in the string
for (char c : chars) {
System.out.print(c);
}
System.out.println("");
for (int i = 0; i < chars.length; ++i) {
if (i % 2 == 0) {
chars[i] = '#';
}
}
String modified = String.valueOf(chars); //Create a new string with a deep copy of chars array
//Essentially calls new String(chars)
System.out.println("Modified: " + modified);
}
private static void testOtherMethods() {
String str1 = "allen chin is a hero";
int pos1 = str1.indexOf('a');
//Should be 0
System.out.println("Index of a in " + str1 + " after 0 is " + pos1);
int pos2 = str1.indexOf('a', 3);
//Should be 14
System.out.println("Index of a in " + str1 + " after 3 is " + pos2);
int pos3 = str1.indexOf('z');
//Should be -1
System.out.println("Index of z in " + str1 + " is " + pos3);
}
private static void testTrimming() {
String str1 = " asdf ";
String str2 = "f sdfs ";
String str3 = " jj./asf/fd ";
System.out.println("str1.trim()=" + str1.trim() + '#');
System.out.println("str2.trim()=" + str2.trim() + '#');
System.out.println("str3.trim()=" + str3.trim() + '#');
}
private static void testSubstring() {
String s = "allen";
System.out.println("Testing substrings of " + s);
System.out.println("0, 1: " + s.substring(0, 1));
System.out.println("0, 0: " + s.substring(0, 0));
System.out.println("4, : " + s.substring(4));
System.out.println("4, 4: " + s.substring(4, 4));
System.out.println("5, : " + s.substring(5));
}
private static void testParsing() {
String s = "104"; //Throw error if there are blanks in the string
System.out.println(Integer.parseInt(s));
int i = -67;
s = String.valueOf(i);
System.out.println(s);
}
private static void testSplit() {
System.out.println("Testing String.split()..");
String[] strs = new String[] {
"/home/allen/haha",
"/haha/dd/",
"//asd/",
"/ha",
"/haha//",
"/"
};
for (String str : strs) {
String[] splitted = str.split("/");
System.out.println("Original string = " + str);
System.out.println("Splited = " + Arrays.toString(splitted));
System.out.println("");
}
}
public static void main(String[] args) {
stringDemo();
System.out.println("");
stringBuilderDemo();
modifyString("Allen Chin");
testOtherMethods();
testTrimming();
testSubstring();
testParsing();
testSplit();
}
}