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