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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/**
* A utility class which contains common and useful String manipulation methods.
* Wrap and Join methods are taken from <a href=
* "http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/WordUtils.html"
* >org.apache.commons.lang.WordUtils</a>
* <p>
* isEmpty() method is taken from <a
* href="http://developer.android.com/reference/android/text/TextUtils.html"
* >android.text.TextUtils</a>
*
*/
public class StringUtils {
/**
* Returns true if the string is null or 0-length.
*
* @param str
* the string to be examined
* @return true if str is null or zero length
*/
public static boolean isEmpty(CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}
/**
* Replaces a null String with an empty string ("")
*
* @param str
* source string
*/
public static String replaceNullWithEmpty(String str) {
return isEmpty(str) ? "" : str;
}
/**
* Replaces an empty string ("") with null
*
* @param str
* source string
*/
public static String replaceEmptyWithNull(String str) {
return isEmpty(str) ? null : str;
}
/**
* Truncates s to fit within len. If s is an empty string, it will return an
* empty string. If s is null, null is returned.
*
* @param s
* source string
* @param len
* number of characters we want
**/
public static String truncate(String s, int len) {
if (s == null) {
return null;
}
if (s.equals("")) {
return "";
}
return s.substring(0, Math.min(len, s.length()));
}
/**
* <p>
* Wraps a single line of text, identifying words by <code>' '</code>.
* </p>
*
* <p>
* New lines will be separated by the system property line separator. Very
* long words, such as URLs will <i>not</i> be wrapped.
* </p>
*
* <p>
* Leading spaces on a new line are stripped. Trailing spaces are not
* stripped.
* </p>
*
* <pre>
* StringUtils.wrap(null, *) = null
* StringUtils.wrap("", *) = ""
* </pre>
*
* This method is a copy of org.apache.commons.lang.WordUtils.wrap() method
*
* @param str
* the String to be word wrapped, may be null
* @param wrapLength
* the column to wrap the words at, less than 1 is treated as 1
* @return a line with newlines inserted, <code>null</code> if null input
*/
public static String wrap(String str, int wrapLength) {
return wrap(str, wrapLength, null, false);
}
/**
* <p>
* Wraps a single line of text, identifying words by <code>' '</code>.
* </p>
*
* <p>
* Leading spaces on a new line are stripped. Trailing spaces are not
* stripped.
* </p>
*
* <pre>
* StringUtils.wrap(null, *, *, *) = null
* StringUtils.wrap("", *, *, *) = ""
* </pre>
*
* This method is a copy of org.apache.commons.lang.WordUtils.wrap() method
*
* @param str
* the String to be word wrapped, may be null
* @param wrapLength
* the column to wrap the words at, less than 1 is treated as 1
* @param newLineStr
* the string to insert for a new line, <code>null</code> uses
* the system property line separator
* @param wrapLongWords
* true if long words (such as URLs) should be wrapped
* @return a line with newlines inserted, <code>null</code> if null input
*/
public static String wrap(String str, int wrapLength, String newLineStr,
boolean wrapLongWords) {
if (str == null) {
return null;
}
if (newLineStr == null) {
newLineStr = "\n";
}
if (wrapLength < 1) {
wrapLength = 1;
}
int inputLineLength = str.length();
int offset = 0;
StringBuffer wrappedLine = new StringBuffer(inputLineLength + 32);
while ((inputLineLength - offset) > wrapLength) {
if (str.charAt(offset) == ' ') {
offset++;
continue;
}
int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);
if (spaceToWrapAt >= offset) {
// normal case
wrappedLine.append(str.substring(offset, spaceToWrapAt));
wrappedLine.append(newLineStr);
offset = spaceToWrapAt + 1;
} else {
// really long word or URL
if (wrapLongWords) {
// wrap really long word one line at a time
wrappedLine.append(str.substring(offset, wrapLength
+ offset));
wrappedLine.append(newLineStr);
offset += wrapLength;
} else {
// do not wrap really long word, just extend beyond limit
spaceToWrapAt = str.indexOf(' ', wrapLength + offset);
if (spaceToWrapAt >= 0) {
wrappedLine
.append(str.substring(offset, spaceToWrapAt));
wrappedLine.append(newLineStr);
offset = spaceToWrapAt + 1;
} else {
wrappedLine.append(str.substring(offset));
offset = inputLineLength;
}
}
}
}
// Whatever is left in line is short enough to just pass through
wrappedLine.append(str.substring(offset));
return wrappedLine.toString();
}
/**
* <p>
* Joins the elements of the provided array into a single String containing
* the provided list of elements.
* </p>
*
* <p>
* No delimiter is added before or after the list. Null objects or empty
* strings within the array are represented by empty strings.
* </p>
*
* <pre>
* StringUtils.join(null, *) = null
* StringUtils.join([], *) = ""
* StringUtils.join([null], *) = ""
* StringUtils.join(["a", "b", "c"], ';') = "a;b;c"
* StringUtils.join(["a", "b", "c"], null) = "abc"
* StringUtils.join([null, "", "a"], ';') = ";;a"
* </pre>
*
* @param array
* the array of values to join together, may be null
* @param separator
* the separator character to use
* @return the joined String, <code>null</code> if null array input
* @since 2.0
*/
public static String join(long[] array, char separator) {
if (array == null) {
return null;
}
return join(array, separator, 0, array.length);
}
/**
* <p>
* Joins the elements of the provided array into a single String containing
* the provided list of elements.
* </p>
*
* <p>
* No delimiter is added before or after the list. Null objects or empty
* strings within the array are represented by empty strings.
* </p>
*
* <pre>
* StringUtils.join(null, *) = null
* StringUtils.join([], *) = ""
* StringUtils.join([null], *) = ""
* StringUtils.join(["a", "b", "c"], ';') = "a;b;c"
* StringUtils.join(["a", "b", "c"], null) = "abc"
* StringUtils.join([null, "", "a"], ';') = ";;a"
* </pre>
*
* @param array
* the array of values to join together, may be null
* @param separator
* the separator character to use
* @param startIndex
* the first index to start joining from. It is an error to pass
* in an end index past the end of the array
* @param endIndex
* the index to stop joining from (exclusive). It is an error to
* pass in an end index past the end of the array
* @return the joined String, <code>null</code> if null array input
* @since 2.0
*/
public static String join(long[] array, char separator, int startIndex,
int endIndex) {
if (array == null) {
return null;
}
int bufSize = (endIndex - startIndex);
if (bufSize <= 0) {
return "";
}
StringBuffer buf = new StringBuffer(bufSize);
for (int i = startIndex; i < endIndex; i++) {
if (i > startIndex) {
buf.append(separator);
}
buf.append(array[i]);
}
return buf.toString();
}
/**
* <p>
* Joins the elements of the provided array into a single String containing
* the provided list of elements.
* </p>
*
* <p>
* No delimiter is added before or after the list. Null objects or empty
* strings within the array are represented by empty strings.
* </p>
*
* <pre>
* StringUtils.join(null, *) = null
* StringUtils.join([], *) = ""
* StringUtils.join([null], *) = ""
* StringUtils.join(["a", "b", "c"], ';') = "a;b;c"
* StringUtils.join(["a", "b", "c"], null) = "abc"
* StringUtils.join([null, "", "a"], ';') = ";;a"
* </pre>
*
* @param array
* the array of values to join together, may be null
* @param separator
* the separator character to use
* @return the joined String, <code>null</code> if null array input
* @since 2.0
*/
public static String join(Object[] array, char separator) {
if (array == null) {
return null;
}
return join(array, separator, 0, array.length);
}
/**
* <p>
* Joins the elements of the provided array into a single String containing
* the provided list of elements.
* </p>
*
* <p>
* No delimiter is added before or after the list. Null objects or empty
* strings within the array are represented by empty strings.
* </p>
*
* <pre>
* StringUtils.join(null, *) = null
* StringUtils.join([], *) = ""
* StringUtils.join([null], *) = ""
* StringUtils.join(["a", "b", "c"], ';') = "a;b;c"
* StringUtils.join(["a", "b", "c"], null) = "abc"
* StringUtils.join([null, "", "a"], ';') = ";;a"
* </pre>
*
* @param array
* the array of values to join together, may be null
* @param separator
* the separator character to use
* @param startIndex
* the first index to start joining from. It is an error to pass
* in an end index past the end of the array
* @param endIndex
* the index to stop joining from (exclusive). It is an error to
* pass in an end index past the end of the array
* @return the joined String, <code>null</code> if null array input
* @since 2.0
*/
public static String join(Object[] array, char separator, int startIndex,
int endIndex) {
if (array == null) {
return null;
}
int bufSize = (endIndex - startIndex);
if (bufSize <= 0) {
return "";
}
bufSize *= ((array[startIndex] == null ? 16 : array[startIndex]
.toString().length()) + 1);
StringBuffer buf = new StringBuffer(bufSize);
for (int i = startIndex; i < endIndex; i++) {
if (i > startIndex) {
buf.append(separator);
}
if (array[i] != null) {
buf.append(array[i]);
}
}
return buf.toString();
}
}
|