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 package org.codehaus.plexus.util;
55
56 import java.util.Arrays;
57 import java.util.Iterator;
58 import java.util.Locale;
59 import java.util.Map;
60 import java.util.Objects;
61 import java.util.StringTokenizer;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public class StringUtils
86 {
87
88
89
90
91
92
93
94
95
96 public StringUtils()
97 {
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 public static String clean( String str )
114 {
115 return ( str == null ? "" : str.trim() );
116 }
117
118
119
120
121
122
123
124
125
126
127
128 public static String trim( String str )
129 {
130 return ( str == null ? null : str.trim() );
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144 public static String deleteWhitespace( String str )
145 {
146 StringBuilder buffer = new StringBuilder();
147 int sz = str.length();
148 for ( int i = 0; i < sz; i++ )
149 {
150 if ( !Character.isWhitespace( str.charAt( i ) ) )
151 {
152 buffer.append( str.charAt( i ) );
153 }
154 }
155 return buffer.toString();
156 }
157
158
159
160
161
162
163
164 public static boolean isNotEmpty( String str )
165 {
166 return ( ( str != null ) && ( !str.isEmpty() ) );
167 }
168
169
170
171
172
173
174
175
176
177
178
179 public static boolean isEmpty( String str )
180 {
181 return ( ( str == null ) || ( str.isEmpty() ) );
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public static boolean isBlank( String str )
202 {
203 int strLen;
204 if ( str == null || ( strLen = str.length() ) == 0 )
205 {
206 return true;
207 }
208 for ( int i = 0; i < strLen; i++ )
209 {
210 if ( !Character.isWhitespace( str.charAt( i ) ) )
211 {
212 return false;
213 }
214 }
215 return true;
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235 public static boolean isNotBlank( String str )
236 {
237 return !StringUtils.isBlank( str );
238 }
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258 @Deprecated
259 public static boolean equals( String str1, String str2 )
260 {
261 return Objects.equals( str1, str2 );
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 public static boolean equalsIgnoreCase( String str1, String str2 )
279 {
280 return ( str1 == null ? str2 == null : str1.equalsIgnoreCase( str2 ) );
281 }
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296 public static int indexOfAny( String str, String[] searchStrs )
297 {
298 if ( ( str == null ) || ( searchStrs == null ) )
299 {
300 return -1;
301 }
302 int sz = searchStrs.length;
303
304
305 int ret = Integer.MAX_VALUE;
306
307 int tmp;
308 for ( String searchStr : searchStrs )
309 {
310 tmp = str.indexOf( searchStr );
311 if ( tmp == -1 )
312 {
313 continue;
314 }
315
316 if ( tmp < ret )
317 {
318 ret = tmp;
319 }
320 }
321
322 return ( ret == Integer.MAX_VALUE ) ? -1 : ret;
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 public static int lastIndexOfAny( String str, String[] searchStrs )
339 {
340 if ( ( str == null ) || ( searchStrs == null ) )
341 {
342 return -1;
343 }
344 int ret = -1;
345 int tmp;
346 for ( String searchStr : searchStrs )
347 {
348 tmp = str.lastIndexOf( searchStr );
349 if ( tmp > ret )
350 {
351 ret = tmp;
352 }
353 }
354 return ret;
355 }
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 public static String substring( String str, int start )
374 {
375 if ( str == null )
376 {
377 return null;
378 }
379
380
381 if ( start < 0 )
382 {
383 start = str.length() + start;
384 }
385
386 if ( start < 0 )
387 {
388 start = 0;
389 }
390 if ( start > str.length() )
391 {
392 return "";
393 }
394
395 return str.substring( start );
396 }
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413 public static String substring( String str, int start, int end )
414 {
415 if ( str == null )
416 {
417 return null;
418 }
419
420
421 if ( end < 0 )
422 {
423 end = str.length() + end;
424 }
425 if ( start < 0 )
426 {
427 start = str.length() + start;
428 }
429
430
431 if ( end > str.length() )
432 {
433
434 end = str.length();
435 }
436
437
438 if ( start > end )
439 {
440 return "";
441 }
442
443 if ( start < 0 )
444 {
445 start = 0;
446 }
447 if ( end < 0 )
448 {
449 end = 0;
450 }
451
452 return str.substring( start, end );
453 }
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469 public static String left( String str, int len )
470 {
471 if ( len < 0 )
472 {
473 throw new IllegalArgumentException( "Requested String length " + len + " is less than zero" );
474 }
475 if ( ( str == null ) || ( str.length() <= len ) )
476 {
477 return str;
478 }
479 else
480 {
481 return str.substring( 0, len );
482 }
483 }
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499 public static String right( String str, int len )
500 {
501 if ( len < 0 )
502 {
503 throw new IllegalArgumentException( "Requested String length " + len + " is less than zero" );
504 }
505 if ( ( str == null ) || ( str.length() <= len ) )
506 {
507 return str;
508 }
509 else
510 {
511 return str.substring( str.length() - len );
512 }
513 }
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531 public static String mid( String str, int pos, int len )
532 {
533 if ( ( pos < 0 ) || ( ( str != null ) && ( pos > str.length() ) ) )
534 {
535 throw new StringIndexOutOfBoundsException( "String index " + pos + " is out of bounds" );
536 }
537 if ( len < 0 )
538 {
539 throw new IllegalArgumentException( "Requested String length " + len + " is less than zero" );
540 }
541 if ( str == null )
542 {
543 return null;
544 }
545 if ( str.length() <= ( pos + len ) )
546 {
547 return str.substring( pos );
548 }
549 else
550 {
551 return str.substring( pos, pos + len );
552 }
553 }
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569 public static String[] split( String str )
570 {
571 return split( str, null, -1 );
572 }
573
574
575
576
577
578
579 public static String[] split( String text, String separator )
580 {
581 return split( text, separator, -1 );
582 }
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602 public static String[] split( String str, String separator, int max )
603 {
604 StringTokenizer tok;
605 if ( separator == null )
606 {
607
608
609 tok = new StringTokenizer( str );
610 }
611 else
612 {
613 tok = new StringTokenizer( str, separator );
614 }
615
616 int listSize = tok.countTokens();
617 if ( ( max > 0 ) && ( listSize > max ) )
618 {
619 listSize = max;
620 }
621
622 String[] list = new String[listSize];
623 int i = 0;
624 int lastTokenBegin;
625 int lastTokenEnd = 0;
626 while ( tok.hasMoreTokens() )
627 {
628 if ( ( max > 0 ) && ( i == listSize - 1 ) )
629 {
630
631
632
633 String endToken = tok.nextToken();
634 lastTokenBegin = str.indexOf( endToken, lastTokenEnd );
635 list[i] = str.substring( lastTokenBegin );
636 break;
637 }
638 else
639 {
640 list[i] = tok.nextToken();
641 lastTokenBegin = str.indexOf( list[i], lastTokenEnd );
642 lastTokenEnd = lastTokenBegin + list[i].length();
643 }
644 i++;
645 }
646 return list;
647 }
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662 public static String concatenate( Object[] array )
663 {
664 return join( array, "" );
665 }
666
667
668
669
670
671
672
673
674
675
676
677
678
679 public static String join( Object[] array, String separator )
680 {
681 if ( separator == null )
682 {
683 separator = "";
684 }
685 int arraySize = array.length;
686 int bufSize = ( arraySize == 0 ? 0 : ( array[0].toString().length() + separator.length() ) * arraySize );
687 StringBuilder buf = new StringBuilder( bufSize );
688
689 for ( int i = 0; i < arraySize; i++ )
690 {
691 if ( i > 0 )
692 {
693 buf.append( separator );
694 }
695 buf.append( array[i] );
696 }
697 return buf.toString();
698 }
699
700
701
702
703
704
705
706
707
708
709
710
711
712 public static String join( Iterator<?> iterator, String separator )
713 {
714 if ( separator == null )
715 {
716 separator = "";
717 }
718 StringBuilder buf = new StringBuilder( 256 );
719 while ( iterator.hasNext() )
720 {
721 buf.append( iterator.next() );
722 if ( iterator.hasNext() )
723 {
724 buf.append( separator );
725 }
726 }
727 return buf.toString();
728 }
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747 public static String replaceOnce( String text, char repl, char with )
748 {
749 return replace( text, repl, with, 1 );
750 }
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766 public static String replace( String text, char repl, char with )
767 {
768 return replace( text, repl, with, -1 );
769 }
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786 public static String replace( String text, char repl, char with, int max )
787 {
788 return replace( text, String.valueOf( repl ), String.valueOf( with ), max );
789 }
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805 public static String replaceOnce( String text, String repl, String with )
806 {
807 return replace( text, repl, with, 1 );
808 }
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824 public static String replace( String text, String repl, String with )
825 {
826 return replace( text, repl, with, -1 );
827 }
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844 public static String replace( String text, String repl, String with, int max )
845 {
846 if ( ( text == null ) || ( repl == null ) || ( with == null ) || ( repl.length() == 0 ) )
847 {
848 return text;
849 }
850
851 StringBuilder buf = new StringBuilder( text.length() );
852 int start = 0, end;
853 while ( ( end = text.indexOf( repl, start ) ) != -1 )
854 {
855 buf.append( text, start, end ).append( with );
856 start = end + repl.length();
857
858 if ( --max == 0 )
859 {
860 break;
861 }
862 }
863 buf.append( text, start, text.length() );
864 return buf.toString();
865 }
866
867
868
869
870
871
872
873
874
875
876
877
878
879 public static String overlayString( String text, String overlay, int start, int end )
880 {
881 return new StringBuilder( start + overlay.length() + text.length() - end
882 + 1 ).append( text, 0, start ).append( overlay ).append( text, end, text.length() ).toString();
883 }
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901 public static String center( String str, int size )
902 {
903 return center( str, size, " " );
904 }
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921 public static String center( String str, int size, String delim )
922 {
923 int sz = str.length();
924 int p = size - sz;
925 if ( p < 1 )
926 {
927 return str;
928 }
929 str = leftPad( str, sz + p / 2, delim );
930 str = rightPad( str, size, delim );
931 return str;
932 }
933
934
935
936
937
938
939
940
941
942
943
944
945
946 public static String chomp( String str )
947 {
948 return chomp( str, "\n" );
949 }
950
951
952
953
954
955
956
957
958
959
960
961 public static String chomp( String str, String sep )
962 {
963 int idx = str.lastIndexOf( sep );
964 if ( idx != -1 )
965 {
966 return str.substring( 0, idx );
967 }
968 else
969 {
970 return str;
971 }
972 }
973
974
975
976
977
978
979
980
981
982
983 public static String chompLast( String str )
984 {
985 return chompLast( str, "\n" );
986 }
987
988
989
990
991
992
993
994
995
996
997
998 public static String chompLast( String str, String sep )
999 {
1000 if ( str.length() == 0 )
1001 {
1002 return str;
1003 }
1004 String sub = str.substring( str.length() - sep.length() );
1005 if ( sep.equals( sub ) )
1006 {
1007 return str.substring( 0, str.length() - sep.length() );
1008 }
1009 else
1010 {
1011 return str;
1012 }
1013 }
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025 public static String getChomp( String str, String sep )
1026 {
1027 int idx = str.lastIndexOf( sep );
1028 if ( idx == str.length() - sep.length() )
1029 {
1030 return sep;
1031 }
1032 else if ( idx != -1 )
1033 {
1034 return str.substring( idx );
1035 }
1036 else
1037 {
1038 return "";
1039 }
1040 }
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052 public static String prechomp( String str, String sep )
1053 {
1054 int idx = str.indexOf( sep );
1055 if ( idx != -1 )
1056 {
1057 return str.substring( idx + sep.length() );
1058 }
1059 else
1060 {
1061 return str;
1062 }
1063 }
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075 public static String getPrechomp( String str, String sep )
1076 {
1077 int idx = str.indexOf( sep );
1078 if ( idx != -1 )
1079 {
1080 return str.substring( 0, idx + sep.length() );
1081 }
1082 else
1083 {
1084 return "";
1085 }
1086 }
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103 public static String chop( String str )
1104 {
1105 if ( "".equals( str ) )
1106 {
1107 return "";
1108 }
1109 if ( str.length() == 1 )
1110 {
1111 return "";
1112 }
1113 int lastIdx = str.length() - 1;
1114 String ret = str.substring( 0, lastIdx );
1115 char last = str.charAt( lastIdx );
1116 if ( last == '\n' )
1117 {
1118 if ( ret.charAt( lastIdx - 1 ) == '\r' )
1119 {
1120 return ret.substring( 0, lastIdx - 1 );
1121 }
1122 }
1123 return ret;
1124 }
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136 public static String chopNewline( String str )
1137 {
1138 int lastIdx = str.length() - 1;
1139 char last = str.charAt( lastIdx );
1140 if ( last == '\n' )
1141 {
1142 if ( str.charAt( lastIdx - 1 ) == '\r' )
1143 {
1144 lastIdx--;
1145 }
1146 }
1147 else
1148 {
1149 lastIdx++;
1150 }
1151 return str.substring( 0, lastIdx );
1152 }
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170 public static String escape( String str )
1171 {
1172
1173
1174 int sz = str.length();
1175 StringBuilder buffer = new StringBuilder( 2 * sz );
1176 for ( int i = 0; i < sz; i++ )
1177 {
1178 char ch = str.charAt( i );
1179
1180
1181 if ( ch > 0xfff )
1182 {
1183 buffer.append( "\\u" + Integer.toHexString( ch ) );
1184 }
1185 else if ( ch > 0xff )
1186 {
1187 buffer.append( "\\u0" + Integer.toHexString( ch ) );
1188 }
1189 else if ( ch > 0x7f )
1190 {
1191 buffer.append( "\\u00" + Integer.toHexString( ch ) );
1192 }
1193 else if ( ch < 32 )
1194 {
1195 switch ( ch )
1196 {
1197 case '\b':
1198 buffer.append( '\\' );
1199 buffer.append( 'b' );
1200 break;
1201 case '\n':
1202 buffer.append( '\\' );
1203 buffer.append( 'n' );
1204 break;
1205 case '\t':
1206 buffer.append( '\\' );
1207 buffer.append( 't' );
1208 break;
1209 case '\f':
1210 buffer.append( '\\' );
1211 buffer.append( 'f' );
1212 break;
1213 case '\r':
1214 buffer.append( '\\' );
1215 buffer.append( 'r' );
1216 break;
1217 default:
1218 if ( ch > 0xf )
1219 {
1220 buffer.append( "\\u00" + Integer.toHexString( ch ) );
1221 }
1222 else
1223 {
1224 buffer.append( "\\u000" + Integer.toHexString( ch ) );
1225 }
1226 break;
1227 }
1228 }
1229 else
1230 {
1231 switch ( ch )
1232 {
1233 case '\'':
1234 buffer.append( '\\' );
1235 buffer.append( '\'' );
1236 break;
1237 case '"':
1238 buffer.append( '\\' );
1239 buffer.append( '"' );
1240 break;
1241 case '\\':
1242 buffer.append( '\\' );
1243 buffer.append( '\\' );
1244 break;
1245 default:
1246 buffer.append( ch );
1247 break;
1248 }
1249 }
1250 }
1251 return buffer.toString();
1252 }
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268 public static String repeat( String str, int repeat )
1269 {
1270 StringBuilder buffer = new StringBuilder( repeat * str.length() );
1271 for ( int i = 0; i < repeat; i++ )
1272 {
1273 buffer.append( str );
1274 }
1275 return buffer.toString();
1276 }
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291 public static String rightPad( String str, int size )
1292 {
1293 return rightPad( str, size, " " );
1294 }
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311 public static String rightPad( String str, int size, String delim )
1312 {
1313 size = ( size - str.length() ) / delim.length();
1314 if ( size > 0 )
1315 {
1316 str += repeat( delim, size );
1317 }
1318 return str;
1319 }
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334 public static String leftPad( String str, int size )
1335 {
1336 return leftPad( str, size, " " );
1337 }
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349 public static String leftPad( String str, int size, String delim )
1350 {
1351 size = ( size - str.length() ) / delim.length();
1352 if ( size > 0 )
1353 {
1354 str = repeat( delim, size ) + str;
1355 }
1356 return str;
1357 }
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370 public static String strip( String str )
1371 {
1372 return strip( str, null );
1373 }
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387 public static String strip( String str, String delim )
1388 {
1389 str = stripStart( str, delim );
1390 return stripEnd( str, delim );
1391 }
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401 public static String[] stripAll( String[] strs )
1402 {
1403 return stripAll( strs, null );
1404 }
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415 public static String[] stripAll( String[] strs, String delimiter )
1416 {
1417 if ( ( strs == null ) || ( strs.length == 0 ) )
1418 {
1419 return strs;
1420 }
1421 int sz = strs.length;
1422 String[] newArr = new String[sz];
1423 for ( int i = 0; i < sz; i++ )
1424 {
1425 newArr[i] = strip( strs[i], delimiter );
1426 }
1427 return newArr;
1428 }
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442 public static String stripEnd( String str, String strip )
1443 {
1444 if ( str == null )
1445 {
1446 return null;
1447 }
1448 int end = str.length();
1449
1450 if ( strip == null )
1451 {
1452 while ( ( end != 0 ) && Character.isWhitespace( str.charAt( end - 1 ) ) )
1453 {
1454 end--;
1455 }
1456 }
1457 else
1458 {
1459 while ( ( end != 0 ) && ( strip.indexOf( str.charAt( end - 1 ) ) != -1 ) )
1460 {
1461 end--;
1462 }
1463 }
1464 return str.substring( 0, end );
1465 }
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479 public static String stripStart( String str, String strip )
1480 {
1481 if ( str == null )
1482 {
1483 return null;
1484 }
1485
1486 int start = 0;
1487
1488 int sz = str.length();
1489
1490 if ( strip == null )
1491 {
1492 while ( ( start != sz ) && Character.isWhitespace( str.charAt( start ) ) )
1493 {
1494 start++;
1495 }
1496 }
1497 else
1498 {
1499 while ( ( start != sz ) && ( strip.indexOf( str.charAt( start ) ) != -1 ) )
1500 {
1501 start++;
1502 }
1503 }
1504 return str.substring( start );
1505 }
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518 public static String upperCase( String str )
1519 {
1520 if ( str == null )
1521 {
1522 return null;
1523 }
1524 return str.toUpperCase();
1525 }
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535 public static String lowerCase( String str )
1536 {
1537 if ( str == null )
1538 {
1539 return null;
1540 }
1541 return str.toLowerCase();
1542 }
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555 public static String uncapitalise( String str )
1556 {
1557 if ( str == null )
1558 {
1559 return null;
1560 }
1561 else if ( str.length() == 0 )
1562 {
1563 return "";
1564 }
1565 else
1566 {
1567 return new StringBuilder( str.length() ).append( Character.toLowerCase( str.charAt( 0 ) ) ).append( str, 1,
1568 str.length() ).toString();
1569 }
1570 }
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583 public static String capitalise( String str )
1584 {
1585 if ( str == null )
1586 {
1587 return null;
1588 }
1589 else if ( str.length() == 0 )
1590 {
1591 return "";
1592 }
1593 else
1594 {
1595 return new StringBuilder( str.length() ).append( Character.toTitleCase( str.charAt( 0 ) ) ).append( str, 1,
1596 str.length() ).toString();
1597 }
1598 }
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614 public static String swapCase( String str )
1615 {
1616 if ( str == null )
1617 {
1618 return null;
1619 }
1620 int sz = str.length();
1621 StringBuilder buffer = new StringBuilder( sz );
1622
1623 boolean whitespace = false;
1624 char ch;
1625 char tmp;
1626
1627 for ( int i = 0; i < sz; i++ )
1628 {
1629 ch = str.charAt( i );
1630 if ( Character.isUpperCase( ch ) )
1631 {
1632 tmp = Character.toLowerCase( ch );
1633 }
1634 else if ( Character.isTitleCase( ch ) )
1635 {
1636 tmp = Character.toLowerCase( ch );
1637 }
1638 else if ( Character.isLowerCase( ch ) )
1639 {
1640 if ( whitespace )
1641 {
1642 tmp = Character.toTitleCase( ch );
1643 }
1644 else
1645 {
1646 tmp = Character.toUpperCase( ch );
1647 }
1648 }
1649 else
1650 {
1651 tmp = ch;
1652 }
1653 buffer.append( tmp );
1654 whitespace = Character.isWhitespace( ch );
1655 }
1656 return buffer.toString();
1657 }
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673 public static String capitaliseAllWords( String str )
1674 {
1675 if ( str == null )
1676 {
1677 return null;
1678 }
1679 int sz = str.length();
1680 StringBuilder buffer = new StringBuilder( sz );
1681 boolean space = true;
1682 for ( int i = 0; i < sz; i++ )
1683 {
1684 char ch = str.charAt( i );
1685 if ( Character.isWhitespace( ch ) )
1686 {
1687 buffer.append( ch );
1688 space = true;
1689 }
1690 else if ( space )
1691 {
1692 buffer.append( Character.toTitleCase( ch ) );
1693 space = false;
1694 }
1695 else
1696 {
1697 buffer.append( ch );
1698 }
1699 }
1700 return buffer.toString();
1701 }
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717 public static String uncapitaliseAllWords( String str )
1718 {
1719 if ( str == null )
1720 {
1721 return null;
1722 }
1723 int sz = str.length();
1724 StringBuilder buffer = new StringBuilder( sz );
1725 boolean space = true;
1726 for ( int i = 0; i < sz; i++ )
1727 {
1728 char ch = str.charAt( i );
1729 if ( Character.isWhitespace( ch ) )
1730 {
1731 buffer.append( ch );
1732 space = true;
1733 }
1734 else if ( space )
1735 {
1736 buffer.append( Character.toLowerCase( ch ) );
1737 space = false;
1738 }
1739 else
1740 {
1741 buffer.append( ch );
1742 }
1743 }
1744 return buffer.toString();
1745 }
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763 public static String getNestedString( String str, String tag )
1764 {
1765 return getNestedString( str, tag, tag );
1766 }
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779 public static String getNestedString( String str, String open, String close )
1780 {
1781 if ( str == null )
1782 {
1783 return null;
1784 }
1785 int start = str.indexOf( open );
1786 if ( start != -1 )
1787 {
1788 int end = str.indexOf( close, start + open.length() );
1789 if ( end != -1 )
1790 {
1791 return str.substring( start + open.length(), end );
1792 }
1793 }
1794 return null;
1795 }
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810 public static int countMatches( String str, String sub )
1811 {
1812 if ( sub.equals( "" ) )
1813 {
1814 return 0;
1815 }
1816 if ( str == null )
1817 {
1818 return 0;
1819 }
1820 int count = 0;
1821 int idx = 0;
1822 while ( ( idx = str.indexOf( sub, idx ) ) != -1 )
1823 {
1824 count++;
1825 idx += sub.length();
1826 }
1827 return count;
1828 }
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844 public static boolean isAlpha( String str )
1845 {
1846 if ( str == null )
1847 {
1848 return false;
1849 }
1850 int sz = str.length();
1851 for ( int i = 0; i < sz; i++ )
1852 {
1853 if ( Character.isLetter( str.charAt( i ) ) == false )
1854 {
1855 return false;
1856 }
1857 }
1858 return true;
1859 }
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872 public static boolean isWhitespace( String str )
1873 {
1874 if ( str == null )
1875 {
1876 return false;
1877 }
1878 int sz = str.length();
1879 for ( int i = 0; i < sz; i++ )
1880 {
1881 if ( ( Character.isWhitespace( str.charAt( i ) ) == false ) )
1882 {
1883 return false;
1884 }
1885 }
1886 return true;
1887 }
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900 public static boolean isAlphaSpace( String str )
1901 {
1902 if ( str == null )
1903 {
1904 return false;
1905 }
1906 int sz = str.length();
1907 for ( int i = 0; i < sz; i++ )
1908 {
1909 if ( ( Character.isLetter( str.charAt( i ) ) == false ) && ( str.charAt( i ) != ' ' ) )
1910 {
1911 return false;
1912 }
1913 }
1914 return true;
1915 }
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928 public static boolean isAlphanumeric( String str )
1929 {
1930 if ( str == null )
1931 {
1932 return false;
1933 }
1934 int sz = str.length();
1935 for ( int i = 0; i < sz; i++ )
1936 {
1937 if ( Character.isLetterOrDigit( str.charAt( i ) ) == false )
1938 {
1939 return false;
1940 }
1941 }
1942 return true;
1943 }
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956 public static boolean isAlphanumericSpace( String str )
1957 {
1958 if ( str == null )
1959 {
1960 return false;
1961 }
1962 int sz = str.length();
1963 for ( int i = 0; i < sz; i++ )
1964 {
1965 if ( ( Character.isLetterOrDigit( str.charAt( i ) ) == false ) && ( str.charAt( i ) != ' ' ) )
1966 {
1967 return false;
1968 }
1969 }
1970 return true;
1971 }
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984 public static boolean isNumeric( String str )
1985 {
1986 if ( str == null )
1987 {
1988 return false;
1989 }
1990 int sz = str.length();
1991 for ( int i = 0; i < sz; i++ )
1992 {
1993 if ( Character.isDigit( str.charAt( i ) ) == false )
1994 {
1995 return false;
1996 }
1997 }
1998 return true;
1999 }
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012 public static boolean isNumericSpace( String str )
2013 {
2014 if ( str == null )
2015 {
2016 return false;
2017 }
2018 int sz = str.length();
2019 for ( int i = 0; i < sz; i++ )
2020 {
2021 if ( ( Character.isDigit( str.charAt( i ) ) == false ) && ( str.charAt( i ) != ' ' ) )
2022 {
2023 return false;
2024 }
2025 }
2026 return true;
2027 }
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042 @Deprecated
2043 public static String defaultString( Object obj )
2044 {
2045 return defaultString( obj, "" );
2046 }
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059 @Deprecated
2060 public static String defaultString( Object obj, String defaultString )
2061 {
2062 return Objects.toString( obj, defaultString );
2063 }
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079 public static String reverse( String str )
2080 {
2081 if ( str == null )
2082 {
2083 return null;
2084 }
2085 return new StringBuilder( str ).reverse().toString();
2086 }
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101 public static String reverseDelimitedString( String str, String delimiter )
2102 {
2103
2104
2105 String[] strs = split( str, delimiter );
2106 reverseArray( strs );
2107 return join( strs, delimiter );
2108 }
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120 private static void reverseArray( Object[] array )
2121 {
2122 int i = 0;
2123 int j = array.length - 1;
2124 Object tmp;
2125
2126 while ( j > i )
2127 {
2128 tmp = array[j];
2129 array[j] = array[i];
2130 array[i] = tmp;
2131 j--;
2132 i++;
2133 }
2134 }
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150 public static String abbreviate( String s, int maxWidth )
2151 {
2152 return abbreviate( s, 0, maxWidth );
2153 }
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165 public static String abbreviate( String s, int offset, int maxWidth )
2166 {
2167 if ( maxWidth < 4 )
2168 {
2169 throw new IllegalArgumentException( "Minimum abbreviation width is 4" );
2170 }
2171 if ( s.length() <= maxWidth )
2172 {
2173 return s;
2174 }
2175 if ( offset > s.length() )
2176 {
2177 offset = s.length();
2178 }
2179 if ( ( s.length() - offset ) < ( maxWidth - 3 ) )
2180 {
2181 offset = s.length() - ( maxWidth - 3 );
2182 }
2183 if ( offset <= 4 )
2184 {
2185 return s.substring( 0, maxWidth - 3 ) + "...";
2186 }
2187 if ( maxWidth < 7 )
2188 {
2189 throw new IllegalArgumentException( "Minimum abbreviation width with offset is 7" );
2190 }
2191 if ( ( offset + ( maxWidth - 3 ) ) < s.length() )
2192 {
2193 return "..." + abbreviate( s.substring( offset ), maxWidth - 3 );
2194 }
2195 return "..." + s.substring( s.length() - ( maxWidth - 3 ) );
2196 }
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210 public static String difference( String s1, String s2 )
2211 {
2212 int at = differenceAt( s1, s2 );
2213 if ( at == -1 )
2214 {
2215 return "";
2216 }
2217 return s2.substring( at );
2218 }
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229 public static int differenceAt( String s1, String s2 )
2230 {
2231 int i;
2232 for ( i = 0; ( i < s1.length() ) && ( i < s2.length() ); ++i )
2233 {
2234 if ( s1.charAt( i ) != s2.charAt( i ) )
2235 {
2236 break;
2237 }
2238 }
2239 if ( ( i < s2.length() ) || ( i < s1.length() ) )
2240 {
2241 return i;
2242 }
2243 return -1;
2244 }
2245
2246 public static String interpolate( String text, Map<?, ?> namespace )
2247 {
2248 Iterator<?> keys = namespace.keySet().iterator();
2249
2250 while ( keys.hasNext() )
2251 {
2252 String key = keys.next().toString();
2253
2254 Object obj = namespace.get( key );
2255
2256 if ( obj == null )
2257 {
2258 throw new NullPointerException( "The value of the key '" + key + "' is null." );
2259 }
2260
2261 String value = obj.toString();
2262
2263 text = replace( text, "${" + key + "}", value );
2264
2265 if ( !key.contains( " " ) )
2266 {
2267 text = replace( text, "$" + key, value );
2268 }
2269 }
2270 return text;
2271 }
2272
2273 public static String removeAndHump( String data, String replaceThis )
2274 {
2275 String temp;
2276
2277 StringBuilder out = new StringBuilder();
2278
2279 temp = data;
2280
2281 StringTokenizer st = new StringTokenizer( temp, replaceThis );
2282
2283 while ( st.hasMoreTokens() )
2284 {
2285 String element = (String) st.nextElement();
2286
2287 out.append( capitalizeFirstLetter( element ) );
2288 }
2289
2290 return out.toString();
2291 }
2292
2293 public static String capitalizeFirstLetter( String data )
2294 {
2295 char firstLetter = Character.toTitleCase( data.substring( 0, 1 ).charAt( 0 ) );
2296
2297 String restLetters = data.substring( 1 );
2298
2299 return firstLetter + restLetters;
2300 }
2301
2302 public static String lowercaseFirstLetter( String data )
2303 {
2304 char firstLetter = Character.toLowerCase( data.substring( 0, 1 ).charAt( 0 ) );
2305
2306 String restLetters = data.substring( 1 );
2307
2308 return firstLetter + restLetters;
2309 }
2310
2311 public static String addAndDeHump( String view )
2312 {
2313 StringBuilder sb = new StringBuilder();
2314
2315 for ( int i = 0; i < view.length(); i++ )
2316 {
2317 if ( ( i != 0 ) && Character.isUpperCase( view.charAt( i ) ) )
2318 {
2319 sb.append( '-' );
2320 }
2321
2322 sb.append( view.charAt( i ) );
2323 }
2324
2325 return sb.toString().trim().toLowerCase( Locale.ENGLISH );
2326 }
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347 public static String quoteAndEscape( String source, char quoteChar )
2348 {
2349 return quoteAndEscape( source, quoteChar, new char[] { quoteChar }, new char[] { ' ' }, '\\', false );
2350 }
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364 public static String quoteAndEscape( String source, char quoteChar, char[] quotingTriggers )
2365 {
2366 return quoteAndEscape( source, quoteChar, new char[] { quoteChar }, quotingTriggers, '\\', false );
2367 }
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379 public static String quoteAndEscape( String source, char quoteChar, final char[] escapedChars, char escapeChar,
2380 boolean force )
2381 {
2382 return quoteAndEscape( source, quoteChar, escapedChars, new char[] { ' ' }, escapeChar, force );
2383 }
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395 public static String quoteAndEscape( String source, char quoteChar, final char[] escapedChars,
2396 final char[] quotingTriggers, char escapeChar, boolean force )
2397 {
2398 return quoteAndEscape( source, quoteChar, escapedChars, quotingTriggers, escapeChar + "%s", force );
2399 }
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411 public static String quoteAndEscape( String source, char quoteChar, final char[] escapedChars,
2412 final char[] quotingTriggers, String escapePattern, boolean force )
2413 {
2414 if ( source == null )
2415 {
2416 return null;
2417 }
2418
2419 if ( !force && source.startsWith( Character.toString( quoteChar ) )
2420 && source.endsWith( Character.toString( quoteChar ) ) )
2421 {
2422 return source;
2423 }
2424
2425 String escaped = escape( source, escapedChars, escapePattern );
2426
2427 boolean quote = false;
2428 if ( force )
2429 {
2430 quote = true;
2431 }
2432 else if ( !escaped.equals( source ) )
2433 {
2434 quote = true;
2435 }
2436 else
2437 {
2438 for ( char quotingTrigger : quotingTriggers )
2439 {
2440 if ( escaped.indexOf( quotingTrigger ) > -1 )
2441 {
2442 quote = true;
2443 break;
2444 }
2445 }
2446 }
2447
2448 if ( quote )
2449 {
2450 return quoteChar + escaped + quoteChar;
2451 }
2452
2453 return escaped;
2454 }
2455
2456
2457
2458
2459
2460
2461
2462
2463 public static String escape( String source, final char[] escapedChars, char escapeChar )
2464 {
2465 return escape( source, escapedChars, escapeChar + "%s" );
2466 }
2467
2468
2469
2470
2471
2472
2473
2474
2475 public static String escape( String source, final char[] escapedChars, String escapePattern )
2476 {
2477 if ( source == null )
2478 {
2479 return null;
2480 }
2481
2482 char[] eqc = new char[escapedChars.length];
2483 System.arraycopy( escapedChars, 0, eqc, 0, escapedChars.length );
2484 Arrays.sort( eqc );
2485
2486 StringBuilder buffer = new StringBuilder( source.length() );
2487
2488 for ( int i = 0; i < source.length(); i++ )
2489 {
2490 final char c = source.charAt( i );
2491 int result = Arrays.binarySearch( eqc, c );
2492
2493 if ( result > -1 )
2494 {
2495 buffer.append( String.format( escapePattern, c ) );
2496 }
2497 else
2498 {
2499 buffer.append( c );
2500 }
2501 }
2502
2503 return buffer.toString();
2504 }
2505
2506
2507
2508
2509
2510
2511
2512
2513 public static String removeDuplicateWhitespace( String s )
2514 {
2515 StringBuilder result = new StringBuilder();
2516 int length = s.length();
2517 boolean isPreviousWhiteSpace = false;
2518 for ( int i = 0; i < length; i++ )
2519 {
2520 char c = s.charAt( i );
2521 boolean thisCharWhiteSpace = Character.isWhitespace( c );
2522 if ( !( isPreviousWhiteSpace && thisCharWhiteSpace ) )
2523 {
2524 result.append( c );
2525 }
2526 isPreviousWhiteSpace = thisCharWhiteSpace;
2527 }
2528 return result.toString();
2529 }
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539 public static String unifyLineSeparators( String s )
2540 {
2541 return unifyLineSeparators( s, System.getProperty( "line.separator" ) );
2542 }
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553 public static String unifyLineSeparators( String s, String ls )
2554 {
2555 if ( s == null )
2556 {
2557 return null;
2558 }
2559
2560 if ( ls == null )
2561 {
2562 ls = System.getProperty( "line.separator" );
2563 }
2564
2565 if ( !( ls.equals( "\n" ) || ls.equals( "\r" ) || ls.equals( "\r\n" ) ) )
2566 {
2567 throw new IllegalArgumentException( "Requested line separator is invalid." );
2568 }
2569
2570 int length = s.length();
2571
2572 StringBuilder buffer = new StringBuilder( length );
2573 for ( int i = 0; i < length; i++ )
2574 {
2575 if ( s.charAt( i ) == '\r' )
2576 {
2577 if ( ( i + 1 ) < length && s.charAt( i + 1 ) == '\n' )
2578 {
2579 i++;
2580 }
2581
2582 buffer.append( ls );
2583 }
2584 else if ( s.charAt( i ) == '\n' )
2585 {
2586 buffer.append( ls );
2587 }
2588 else
2589 {
2590 buffer.append( s.charAt( i ) );
2591 }
2592 }
2593
2594 return buffer.toString();
2595 }
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618 public static boolean contains( String str, char searchChar )
2619 {
2620 if ( isEmpty( str ) )
2621 {
2622 return false;
2623 }
2624 return str.indexOf( searchChar ) >= 0;
2625 }
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650 public static boolean contains( String str, String searchStr )
2651 {
2652 if ( str == null || searchStr == null )
2653 {
2654 return false;
2655 }
2656 return str.contains( searchStr );
2657 }
2658 }