-
Notifications
You must be signed in to change notification settings - Fork 65
/
StringUtils.java
312 lines (282 loc) · 7.1 KB
/
StringUtils.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* Data-Structures-In-Java
* StringUtils.java
*/
package com.deepak.data.structures.Utils;
/**
* Utilities for String class
*
* @author Deepak
*/
public class StringUtils {
/**
* Method to check if string is empty
*
* @param cs
* @return {@link boolean}
*/
public static boolean isEmpty(final CharSequence cs) {
/* String is empty is it is null or length is 0 */
return cs == null || cs.length() == 0;
}
/**
* Method to check if any string is empty in an array of strings
*
* @param css
* @return {@link boolean}
*/
public static boolean isAnyEmpty(final CharSequence... css) {
/* Input has to be array */
if (ArrayUtils.isEmpty(css)) {
return false;
}
/* Check for each character */
for (CharSequence cs : css) {
if (isEmpty(cs)) {
return true;
}
}
return false;
}
/**
* Method to check if a string is blank
* Checks if a CharSequence is empty (""), null or whitespace only.
*
* @param cs
* @return {@link boolean}
*/
public static boolean isBlank(final CharSequence cs) {
int strLen;
/* If string is null or length is 0 */
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
/* There should not be any white space character */
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(cs.charAt(i)) == false) {
return false;
}
}
return true;
}
/**
* Method to check if any string blank
*
* @param css
* @return {@link boolean}
*/
public static boolean isAnyBlank(final CharSequence... css) {
/* Input has to be array */
if (ArrayUtils.isEmpty(css)) {
return true;
}
/* Check for each character, if it is blank */
for (CharSequence cs : css) {
if (isBlank(cs)) {
return true;
}
}
return false;
}
/**
* Method to trim the white spaces in a string
*
* @param str
* @return {@link String}
*/
public static String trim(final String str) {
/* If string is null, return null else trim */
return str == null ? null : str.trim();
}
/**
* Method to truncate the string when max width is given
*
* @param str
* @param maxWidth
* @return {@link String}
*/
public static String truncate(final String str, final int maxWidth) {
return truncate(str, 0, maxWidth);
}
/**
* Method to truncate the string when max width
* and offset is given
*
* @param str
* @param offset
* @param maxWidth
* @return {@link String}
*/
public static String truncate(final String str, final int offset, final int maxWidth) {
/* Offset and max width can't be less then 0 */
if (offset < 0) {
throw new IllegalArgumentException("offset cannot be negative");
}
if (maxWidth < 0) {
throw new IllegalArgumentException("maxWidth cannot be negative");
}
/* If string is null, return null */
if (str == null) {
return null;
}
/* If offset is greater then list length, return empty string */
if (offset > str.length()) {
return "";
}
/* If valid scenario, find where we have to stop */
if (str.length() > maxWidth) {
final int ix = offset + maxWidth > str.length() ? str.length() : offset + maxWidth;
return str.substring(offset, ix);
}
return str.substring(offset);
}
/**
* Method to check if two strings are equal
*
* @param cs1
* @param cs2
* @return {@link boolean}
*/
public static boolean equals(final CharSequence cs1, final CharSequence cs2) {
/* If there reference is same, they are equal */
if (cs1 == cs2) {
return true;
}
/* If any one of them is null, they can't be equal */
if (cs1 == null || cs2 == null) {
return false;
}
/* If there lengths are different, they can't be equal */
if (cs1.length() != cs2.length()) {
return false;
}
/* Now, both of them has to be string and equal in value */
if (cs1 instanceof String && cs2 instanceof String) {
return cs1.equals(cs2);
}
return false;
}
/**
* Method to check if two strings are equal when cases are ignored
*
* @param str1
* @param str2
* @return {@link boolean}
*/
public static boolean equalsIgnoreCase(final CharSequence str1, final CharSequence str2) {
String updatedStr1 = String.valueOf(str1).toLowerCase();
String updatedStr2 = String.valueOf(str2).toLowerCase();
return equals(updatedStr1, updatedStr2);
}
/**
* Method to compare two strings
*
* @param str1
* @param str2
* @return {@link int}
*/
public static int compare(final String str1, final String str2) {
return compare(str1, str2, true);
}
/**
* Method to compare two strings when null has to be considered or not
*
* @param str1
* @param str2
* @param shouldConsiderNull
* @return {@link int}
*/
public static int compare(final String str1, final String str2, final boolean shouldConsiderNull) {
/* If both are same, return 0 */
if (str1 == str2) {
return 0;
}
/* If str1 is less, return < 0 */
if (str1 == null) {
return shouldConsiderNull ? -1 : 1;
}
/* If str2 is less, return > 0 */
if (str2 == null) {
return shouldConsiderNull ? 1 : -1;
}
return str1.compareTo(str2);
}
/**
* Method to find the index of first occurrence of character in a string
*
* @param seq
* @param searchChar
* @return {@link int}
*/
public static int indexOf(final CharSequence seq, final int searchChar) {
return indexOf(seq, searchChar, 0);
}
/**
* Method to find the index of a character after a given position
*
* @param seq
* @param searchChar
* @param startPos
* @return {@link int}
*/
public static int indexOf(final CharSequence seq, final int searchChar, final int startPos) {
if (isEmpty(seq)) {
throw new IllegalArgumentException("Invalid character sequence!");
}
if (startPos < seq.length()) {
for (int i = startPos; i < seq.length(); i++) {
/* If a negative start position is given, keep jumping until you reach 0 */
if (i < 0) continue;
if (seq.charAt(i) == searchChar) {
return i;
}
}
}
return -1;
}
public static int lastIndexOf(final CharSequence seq, final int searchChar) {
return 1;
}
public static boolean contains(final CharSequence seq, final int searchChar) {
return false;
}
public static String substring(final String str, int start) {
return str;
}
public static String left(final String str, final int len) {
return str;
}
public static String right(final String str, final int len) {
return str;
}
public static String mid(final String str, int pos, final int len) {
return str;
}
public static String[] split(final String str, final char separatorChar) {
return null;
}
public static String join(final Object[] array, final char separator) {
return null;
}
public static String remove(final String str, final String remove) {
return null;
}
public static String upperCase(final String str) {
return null;
}
public static String lowerCase(final String str) {
return null;
}
public static int countMatches(final CharSequence str, final CharSequence sub) {
return 0;
}
public static boolean isAlpha(final CharSequence cs) {
return false;
}
public static boolean isAlphaNumeric(final CharSequence cs) {
return false;
}
public static boolean isNumeric(final CharSequence cs) {
return false;
}
}