@@ -138,6 +138,7 @@ public static boolean hasText(CharSequence str) {
138
138
if (!hasLength (str )) {
139
139
return false ;
140
140
}
141
+
141
142
int strLen = str .length ();
142
143
for (int i = 0 ; i < strLen ; i ++) {
143
144
if (!Character .isWhitespace (str .charAt (i ))) {
@@ -172,6 +173,7 @@ public static boolean containsWhitespace(CharSequence str) {
172
173
if (!hasLength (str )) {
173
174
return false ;
174
175
}
176
+
175
177
int strLen = str .length ();
176
178
for (int i = 0 ; i < strLen ; i ++) {
177
179
if (Character .isWhitespace (str .charAt (i ))) {
@@ -202,6 +204,7 @@ public static String trimWhitespace(String str) {
202
204
if (!hasLength (str )) {
203
205
return str ;
204
206
}
207
+
205
208
StringBuilder sb = new StringBuilder (str );
206
209
while (sb .length () > 0 && Character .isWhitespace (sb .charAt (0 ))) {
207
210
sb .deleteCharAt (0 );
@@ -223,6 +226,7 @@ public static String trimAllWhitespace(String str) {
223
226
if (!hasLength (str )) {
224
227
return str ;
225
228
}
229
+
226
230
int len = str .length ();
227
231
StringBuilder sb = new StringBuilder (str .length ());
228
232
for (int i = 0 ; i < len ; i ++) {
@@ -244,6 +248,7 @@ public static String trimLeadingWhitespace(String str) {
244
248
if (!hasLength (str )) {
245
249
return str ;
246
250
}
251
+
247
252
StringBuilder sb = new StringBuilder (str );
248
253
while (sb .length () > 0 && Character .isWhitespace (sb .charAt (0 ))) {
249
254
sb .deleteCharAt (0 );
@@ -261,6 +266,7 @@ public static String trimTrailingWhitespace(String str) {
261
266
if (!hasLength (str )) {
262
267
return str ;
263
268
}
269
+
264
270
StringBuilder sb = new StringBuilder (str );
265
271
while (sb .length () > 0 && Character .isWhitespace (sb .charAt (sb .length () - 1 ))) {
266
272
sb .deleteCharAt (sb .length () - 1 );
@@ -278,6 +284,7 @@ public static String trimLeadingCharacter(String str, char leadingCharacter) {
278
284
if (!hasLength (str )) {
279
285
return str ;
280
286
}
287
+
281
288
StringBuilder sb = new StringBuilder (str );
282
289
while (sb .length () > 0 && sb .charAt (0 ) == leadingCharacter ) {
283
290
sb .deleteCharAt (0 );
@@ -295,6 +302,7 @@ public static String trimTrailingCharacter(String str, char trailingCharacter) {
295
302
if (!hasLength (str )) {
296
303
return str ;
297
304
}
305
+
298
306
StringBuilder sb = new StringBuilder (str );
299
307
while (sb .length () > 0 && sb .charAt (sb .length () - 1 ) == trailingCharacter ) {
300
308
sb .deleteCharAt (sb .length () - 1 );
@@ -320,6 +328,7 @@ public static boolean startsWithIgnoreCase(String str, String prefix) {
320
328
if (str .length () < prefix .length ()) {
321
329
return false ;
322
330
}
331
+
323
332
String lcStr = str .substring (0 , prefix .length ()).toLowerCase ();
324
333
String lcPrefix = prefix .toLowerCase ();
325
334
return lcStr .equals (lcPrefix );
@@ -374,6 +383,7 @@ public static int countOccurrencesOf(String str, String sub) {
374
383
if (!hasLength (str ) || !hasLength (sub )) {
375
384
return 0 ;
376
385
}
386
+
377
387
int count = 0 ;
378
388
int pos = 0 ;
379
389
int idx ;
@@ -396,19 +406,29 @@ public static String replace(String inString, String oldPattern, String newPatte
396
406
if (!hasLength (inString ) || !hasLength (oldPattern ) || newPattern == null ) {
397
407
return inString ;
398
408
}
399
- StringBuilder sb = new StringBuilder ();
400
- int pos = 0 ; // our position in the old string
401
409
int index = inString .indexOf (oldPattern );
402
- // the index of an occurrence we've found, or -1
410
+ if (index == -1 ) {
411
+ // no occurrence -> can return input as-is
412
+ return inString ;
413
+ }
414
+
415
+ int capacity = inString .length ();
416
+ if (newPattern .length () > oldPattern .length ()) {
417
+ capacity += 16 ;
418
+ }
419
+ StringBuilder sb = new StringBuilder (capacity );
420
+
421
+ int pos = 0 ; // our position in the old string
403
422
int patLen = oldPattern .length ();
404
423
while (index >= 0 ) {
405
424
sb .append (inString .substring (pos , index ));
406
425
sb .append (newPattern );
407
426
pos = index + patLen ;
408
427
index = inString .indexOf (oldPattern , pos );
409
428
}
429
+
430
+ // append any characters to the right of a match
410
431
sb .append (inString .substring (pos ));
411
- // remember to append any characters to the right of a match
412
432
return sb .toString ();
413
433
}
414
434
@@ -433,7 +453,8 @@ public static String deleteAny(String inString, String charsToDelete) {
433
453
if (!hasLength (inString ) || !hasLength (charsToDelete )) {
434
454
return inString ;
435
455
}
436
- StringBuilder sb = new StringBuilder ();
456
+
457
+ StringBuilder sb = new StringBuilder (inString .length ());
437
458
for (int i = 0 ; i < inString .length (); i ++) {
438
459
char c = inString .charAt (i );
439
460
if (charsToDelete .indexOf (c ) == -1 ) {
@@ -516,6 +537,7 @@ private static String changeFirstCharacterCase(String str, boolean capitalize) {
516
537
if (!hasLength (str )) {
517
538
return str ;
518
539
}
540
+
519
541
char baseChar = str .charAt (0 );
520
542
char updatedChar ;
521
543
if (capitalize ) {
@@ -527,6 +549,7 @@ private static String changeFirstCharacterCase(String str, boolean capitalize) {
527
549
if (baseChar == updatedChar ) {
528
550
return str ;
529
551
}
552
+
530
553
char [] chars = str .toCharArray ();
531
554
chars [0 ] = updatedChar ;
532
555
return new String (chars , 0 , chars .length );
@@ -542,6 +565,7 @@ public static String getFilename(String path) {
542
565
if (path == null ) {
543
566
return null ;
544
567
}
568
+
545
569
int separatorIndex = path .lastIndexOf (FOLDER_SEPARATOR );
546
570
return (separatorIndex != -1 ? path .substring (separatorIndex + 1 ) : path );
547
571
}
@@ -556,14 +580,17 @@ public static String getFilenameExtension(String path) {
556
580
if (path == null ) {
557
581
return null ;
558
582
}
583
+
559
584
int extIndex = path .lastIndexOf (EXTENSION_SEPARATOR );
560
585
if (extIndex == -1 ) {
561
586
return null ;
562
587
}
588
+
563
589
int folderIndex = path .lastIndexOf (FOLDER_SEPARATOR );
564
590
if (folderIndex > extIndex ) {
565
591
return null ;
566
592
}
593
+
567
594
return path .substring (extIndex + 1 );
568
595
}
569
596
@@ -578,14 +605,17 @@ public static String stripFilenameExtension(String path) {
578
605
if (path == null ) {
579
606
return null ;
580
607
}
608
+
581
609
int extIndex = path .lastIndexOf (EXTENSION_SEPARATOR );
582
610
if (extIndex == -1 ) {
583
611
return path ;
584
612
}
613
+
585
614
int folderIndex = path .lastIndexOf (FOLDER_SEPARATOR );
586
615
if (folderIndex > extIndex ) {
587
616
return path ;
588
617
}
618
+
589
619
return path .substring (0 , extIndex );
590
620
}
591
621
@@ -701,8 +731,10 @@ public static Locale parseLocaleString(String localeString) {
701
731
String [] parts = tokenizeToStringArray (localeString , "_ " , false , false );
702
732
String language = (parts .length > 0 ? parts [0 ] : "" );
703
733
String country = (parts .length > 1 ? parts [1 ] : "" );
734
+
704
735
validateLocalePart (language );
705
736
validateLocalePart (country );
737
+
706
738
String variant = "" ;
707
739
if (parts .length > 2 ) {
708
740
// There is definitely a variant, and it is everything after the country
@@ -770,6 +802,7 @@ public static String[] addStringToArray(String[] array, String str) {
770
802
if (ObjectUtils .isEmpty (array )) {
771
803
return new String [] {str };
772
804
}
805
+
773
806
String [] newArr = new String [array .length + 1 ];
774
807
System .arraycopy (array , 0 , newArr , 0 , array .length );
775
808
newArr [array .length ] = str ;
@@ -791,6 +824,7 @@ public static String[] concatenateStringArrays(String[] array1, String[] array2)
791
824
if (ObjectUtils .isEmpty (array2 )) {
792
825
return array1 ;
793
826
}
827
+
794
828
String [] newArr = new String [array1 .length + array2 .length ];
795
829
System .arraycopy (array1 , 0 , newArr , 0 , array1 .length );
796
830
System .arraycopy (array2 , 0 , newArr , array1 .length , array2 .length );
@@ -814,6 +848,7 @@ public static String[] mergeStringArrays(String[] array1, String[] array2) {
814
848
if (ObjectUtils .isEmpty (array2 )) {
815
849
return array1 ;
816
850
}
851
+
817
852
List <String > result = new ArrayList <String >();
818
853
result .addAll (Arrays .asList (array1 ));
819
854
for (String str : array2 ) {
@@ -833,6 +868,7 @@ public static String[] sortStringArray(String[] array) {
833
868
if (ObjectUtils .isEmpty (array )) {
834
869
return new String [0 ];
835
870
}
871
+
836
872
Arrays .sort (array );
837
873
return array ;
838
874
}
@@ -848,6 +884,7 @@ public static String[] toStringArray(Collection<String> collection) {
848
884
if (collection == null ) {
849
885
return null ;
850
886
}
887
+
851
888
return collection .toArray (new String [collection .size ()]);
852
889
}
853
890
@@ -862,6 +899,7 @@ public static String[] toStringArray(Enumeration<String> enumeration) {
862
899
if (enumeration == null ) {
863
900
return null ;
864
901
}
902
+
865
903
List <String > list = Collections .list (enumeration );
866
904
return list .toArray (new String [list .size ()]);
867
905
}
@@ -876,6 +914,7 @@ public static String[] trimArrayElements(String[] array) {
876
914
if (ObjectUtils .isEmpty (array )) {
877
915
return new String [0 ];
878
916
}
917
+
879
918
String [] result = new String [array .length ];
880
919
for (int i = 0 ; i < array .length ; i ++) {
881
920
String element = array [i ];
@@ -894,6 +933,7 @@ public static String[] removeDuplicateStrings(String[] array) {
894
933
if (ObjectUtils .isEmpty (array )) {
895
934
return array ;
896
935
}
936
+
897
937
Set <String > set = new LinkedHashSet <String >();
898
938
for (String element : array ) {
899
939
set .add (element );
@@ -918,6 +958,7 @@ public static String[] split(String toSplit, String delimiter) {
918
958
if (offset < 0 ) {
919
959
return null ;
920
960
}
961
+
921
962
String beforeDelimiter = toSplit .substring (0 , offset );
922
963
String afterDelimiter = toSplit .substring (offset + delimiter .length ());
923
964
return new String [] {beforeDelimiter , afterDelimiter };
@@ -958,6 +999,7 @@ public static Properties splitArrayElementsIntoProperties(
958
999
if (ObjectUtils .isEmpty (array )) {
959
1000
return null ;
960
1001
}
1002
+
961
1003
Properties result = new Properties ();
962
1004
for (String element : array ) {
963
1005
if (charsToDelete != null ) {
@@ -1018,6 +1060,7 @@ public static String[] tokenizeToStringArray(
1018
1060
if (str == null ) {
1019
1061
return null ;
1020
1062
}
1063
+
1021
1064
StringTokenizer st = new StringTokenizer (str , delimiters );
1022
1065
List <String > tokens = new ArrayList <String >();
1023
1066
while (st .hasMoreTokens ()) {
@@ -1071,6 +1114,7 @@ public static String[] delimitedListToStringArray(String str, String delimiter,
1071
1114
if (delimiter == null ) {
1072
1115
return new String [] {str };
1073
1116
}
1117
+
1074
1118
List <String > result = new ArrayList <String >();
1075
1119
if ("" .equals (delimiter )) {
1076
1120
for (int i = 0 ; i < str .length (); i ++) {
@@ -1132,6 +1176,7 @@ public static String collectionToDelimitedString(Collection<?> coll, String deli
1132
1176
if (CollectionUtils .isEmpty (coll )) {
1133
1177
return "" ;
1134
1178
}
1179
+
1135
1180
StringBuilder sb = new StringBuilder ();
1136
1181
Iterator <?> it = coll .iterator ();
1137
1182
while (it .hasNext ()) {
@@ -1178,6 +1223,7 @@ public static String arrayToDelimitedString(Object[] arr, String delim) {
1178
1223
if (arr .length == 1 ) {
1179
1224
return ObjectUtils .nullSafeToString (arr [0 ]);
1180
1225
}
1226
+
1181
1227
StringBuilder sb = new StringBuilder ();
1182
1228
for (int i = 0 ; i < arr .length ; i ++) {
1183
1229
if (i > 0 ) {
0 commit comments