forked from nus-cs2103-AY1819S2/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringUtil.java
160 lines (136 loc) · 6.32 KB
/
StringUtil.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
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
package seedu.address.commons.util;
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
/**
* Helper functions for handling strings.
*/
public class StringUtil {
/**
* Returns true if the {@code sentence} contains the {@code word}.
* Ignores case, but a full word match is required.
* <br>examples:<pre>
* containsWordIgnoreCase("ABc def", "abc") == true
* containsWordIgnoreCase("ABc def", "DEF") == true
* containsWordIgnoreCase("ABc def", "AB") == false //not a full word match
* </pre>
* @param sentence cannot be null
* @param word cannot be null, cannot be empty, must be a single word
*/
public static boolean containsWordIgnoreCase(String sentence, String word) {
requireNonNull(sentence);
requireNonNull(word);
String preppedWord = word.trim();
checkArgument(!preppedWord.isEmpty(), "Word parameter cannot be empty");
checkArgument(preppedWord.split("\\s+").length == 1, "Word parameter should be a single word");
String preppedSentence = sentence.trim();
String[] wordsInPreppedSentence = preppedSentence.split("\\s+");
return Arrays.stream(wordsInPreppedSentence)
.anyMatch(preppedWord::equalsIgnoreCase);
}
/**
* Returns true if the {@code sentence} contains the {@code word}.
* Ignores case, but a full word match is required.
* <br>examples:<pre>
* containsWordCaseSensitive("ABc def", "abc") == true
* containsWordCaseSensitive("ABc def", "DEF") == true
* containsWordCaseSensitive("ABc def", "AB") == false //not a full word match
* </pre>
* @param sentence cannot be null
* @param word cannot be null, cannot be empty, must be a single word
*/
public static boolean containsWordCaseSensitive(String sentence, String word) {
requireNonNull(sentence);
requireNonNull(word);
String preppedWord = word.trim();
checkArgument(!preppedWord.isEmpty(), "Word parameter cannot be empty");
checkArgument(preppedWord.split("\\s+").length == 1, "Word parameter should be a single word");
String preppedSentence = sentence.trim();
String[] wordsInPreppedSentence = preppedSentence.split("\\s+");
return Arrays.stream(wordsInPreppedSentence)
.anyMatch(preppedWord::equals);
}
/**
* Returns true if the {@code sentence} contains the {@code subStr}.
* Ignores case, but substring cannot contain whitespace.
* <br>examples:<pre>
* containsStringIgnoreCase("ABc def", "aB") == true
* containsStringIgnoreCase("ABc def", "Ef") == true
* containsStringIgnoreCase("ABc def", "c de") == false //contains whitespace
* </pre>
* @param sentence cannot be null
* @param subStr cannot be null or empty, must have no whitespaces
*/
public static boolean containsStringIgnoreCase(String sentence, String subStr) {
requireNonNull(sentence);
requireNonNull(subStr);
String preppedSubString = subStr.trim().toLowerCase();
checkArgument(!preppedSubString.isEmpty(), "Substring parameter cannot be empty");
checkArgument(preppedSubString.split("\\s+").length == 1, "Substring parameter should be single word");
String preppedSentence = sentence.trim().toLowerCase();
return preppedSentence.contains(preppedSubString);
}
/**
* Returns true if the {@code sentence} contains the {@code subStr}.
* Ignores case, but substring cannot contain whitespace.
* <br>examples:<pre>
* containsStringCaseSensitive("ABc def", "aB") == true
* containsStringCaseSensitive("ABc def", "Ef") == true
* containsStringCaseSensitive("ABc def", "c de") == false //contains whitespace
* </pre>
* @param sentence cannot be null
* @param subStr cannot be null or empty, must have no whitespaces
*/
public static boolean containsStringCaseSensitive(String sentence, String subStr) {
requireNonNull(sentence);
requireNonNull(subStr);
String preppedSubString = subStr.trim();
checkArgument(!preppedSubString.isEmpty(), "Substring parameter cannot be empty");
checkArgument(preppedSubString.split("\\s+").length == 1, "Substring parameter should be single word");
String preppedSentence = sentence.trim();
return preppedSentence.contains(preppedSubString);
}
/**
* Returns a detailed message of the t, including the stack trace.
*/
public static String getDetails(Throwable t) {
requireNonNull(t);
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
return t.getMessage() + "\n" + sw.toString();
}
/**
* Returns true if {@code s} represents a non-zero unsigned integer
* e.g. 1, 2, 3, ..., {@code Integer.MAX_VALUE} <br>
* Will return false for any other non-null string input
* e.g. empty string, "-1", "0", "+1", and " 2 " (untrimmed), "3 0" (contains whitespace), "1 a" (contains letters)
* @throws NullPointerException if {@code s} is null.
*/
public static boolean isNonZeroUnsignedInteger(String s) {
requireNonNull(s);
try {
int value = Integer.parseInt(s);
return value > 0 && !s.startsWith("+"); // "+1" is successfully parsed by Integer#parseInt(String)
} catch (NumberFormatException nfe) {
return false;
}
}
/**
* Returns true if {@code s} represents an unsigned integer
* e.g. 0, 1, 2, 3, ..., {@code Integer.MAX_VALUE} <br>
* Will return false for any other non-null string input
* e.g. empty string, "-1", "0", "+1", and " 2 " (untrimmed), "3 0" (contains whitespace), "1 a" (contains letters)
* @throws NullPointerException if {@code s} is null.
*/
public static boolean isUnsignedInteger(String s) {
requireNonNull(s);
try {
int value = Integer.parseInt(s);
return value >= 0 && !s.startsWith("+"); // "+1" is successfully parsed by Integer#parseInt(String)
} catch (NumberFormatException nfe) {
return false;
}
}
}