forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtil.java
More file actions
188 lines (154 loc) · 5 KB
/
StringUtil.java
File metadata and controls
188 lines (154 loc) · 5 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
import com.sun.xml.internal.fastinfoset.util.CharArray;
import com.sun.xml.internal.fastinfoset.util.CharArrayString;
public class StringUtil
{
public static void main(String[] args)
{
System.out.println("Welcome to StringUtil");
System.out.println();
getFirstCharacter("Goodbye");
getFirstCharacter("Hello");
System.out.println();
getLastCharacter("Hello");
getLastCharacter("Goodbye");
System.out.println();
System.out.println(getFirstTwoCharacters("Hello"));
System.out.println(getFirstTwoCharacters("Goodbye"));
System.out.println();
System.out.println(getLastTwoCharacters("Goodbye"));
System.out.println(getLastTwoCharacters("Hello"));
System.out.println();
System.out.println(getFirstThreeCharacters("Hello"));
System.out.println(getFirstThreeCharacters("Goodbye"));
System.out.println();
System.out.println(printCharacters("Hello"));
System.out.println(printCharacters("Goodbye"));
System.out.println();
printPhoneNumber("501-555-0100");
printPhoneNumber2("5015550100");
System.out.println();
findFirstE("Hello");
findFirstE("Goodbye");
System.out.println();
System.out.println(isFinn("Finn"));
System.out.println(isFinn("Jake"));
System.out.println();
System.out.println(reverse("Finn"));
System.out.println();
System.out.println(isPalindrome("Finn"));
System.out.println(isPalindrome("dad"));
System.out.println();
String[] wordArray = new String[4];
wordArray[0]="pickle";
wordArray[1]="learn";
wordArray[2]="education";
wordArray[3]="coding";
System.out.println("The result is "+allLetters(wordArray));
}
private static String getFirstCharacter(String value)
{
String word = "Hello";
String newWord = "Goodbye";
System.out.println(word.substring(0, 1));
System.out.println(newWord.substring(0, 1));
return value.substring(0, 1);
}
private static String getLastCharacter(String value)
{
int startIndex = value.length() - 1;
return value.substring(startIndex, startIndex + 1);
}
private static String getFirstTwoCharacters(String value)
{
return value.substring(0, 2);
}
private static String getLastTwoCharacters(String value)
{
int startIndex = value.length() - 2;
return value.substring(startIndex, startIndex + 2);
}
private static String getFirstThreeCharacters(String value)
{
return value.substring(0, 3);
}
private static String printCharacters(String value)
{
for (int i = 0; i < value.length(); i++)
{
char letter = value.charAt(i);
System.out.println(letter + ":" + i);
}
return value;
}
private static void printPhoneNumber(String value)
{
System.out.print("Area Code: " + value.substring(0, 3) + " ");
System.out.print("Exchange: " + value.substring(4, 7) + " ");
System.out.println("Line Number: " + value.substring(8, 12));
System.out.println();
}
//9-S-1
private static void printPhoneNumber2(String value2)
{
System.out.print("Area Code: " + value2.substring(0, 3) + " ");
System.out.print("Exchange: " + value2.substring(3, 6) + " ");
System.out.println("Line Number: " + value2.substring(6, 10));
System.out.println();
}
private static void findFirstE(String e)
{
e = "Hello";
String e2 = "Goodbye";
int index = e.indexOf("e");
int index2 = e2.indexOf("e");
System.out.println("First e in " + e + " is at position: " + index);
System.out.println("First e in " + e2 + " is at position: " + index2);
}
private static boolean isFinn(String value)
{
if (value.equals("Finn"))
{
return true;
} else
{
return false;
}
}
// 9-S-2
private static String reverse(String word)
{
String reverse = new StringBuffer(word).reverse().toString();
return reverse;
}
// 9-S-3
private static boolean isPalindrome(String value)
{
String reverse = new StringBuffer(value).reverse().toString();
if (value.equals(reverse))
{
return true;
} else
{
return false;
}
}
///don't think this is working correctly
private static boolean allLetters(String[] wordArray)
{
String[] word = "learn".split("");
for (int i = 0; i < wordArray.length - 1; i++)
{
for (String words : wordArray)
{
if (words.startsWith(word.toString()))
{
return true;
} else
{
return false;
}
}
}
return false;
}
}