-
Notifications
You must be signed in to change notification settings - Fork 2
/
StringUtils.as
2342 lines (2170 loc) · 77.7 KB
/
StringUtils.as
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2009 the original author or authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.as3commons.lang {
/**
* <p>Methods in this class give sample code to explain their operation.
* The symbol <code>*</code> is used to indicate any input
* including <code>null</code>.</p>
*
* @author Steffen Leistner
* @author Christophe Herreman
*/
public class StringUtils {
/**
* The empty String <code>""</code>
*/
private static const EMPTY:String = '';
/**
* Represents a failed index search.
*/
private static const INDEX_NOT_FOUND:int = -1;
/**
* The control character code
*/
private static const WHITE:uint = 32;
/**
* <p>The maximum size to which the padding constant(s) can expand.</p>
*/
private static const PAD_LIMIT:uint = 8192;
/**
* <p>Returns a simple Initials like string.</p>
*
* StringUtils.toInitials("stringutils") = s
* StringUtils.toInitials("stringUtils") = su
* StringUtils.toInitials("stringUtilsTest") = sut
*/
public static function toInitials(str:String):String {
if (isEmpty(str)) {
return str;
}
return str.match(/[A-Z]/g).join('').toLowerCase();
}
/**
* <p>Removes one newline from end of a String if it's there,
* otherwise leave it alone. A newline is "<code>\n</code>",
* "<code>\r</code>", or "<code>\r\n</code>".</p>
*
* <pre>
* StringUtils.chomp(null) = null
* StringUtils.chomp("") = ""
* StringUtils.chomp("abc \r") = "abc "
* StringUtils.chomp("abc\n") = "abc"
* StringUtils.chomp("abc\r\n") = "abc"
* StringUtils.chomp("abc\r\n\r\n") = "abc\r\n"
* StringUtils.chomp("abc\n\r") = "abc\n"
* StringUtils.chomp("abc\n\rabc") = "abc\n\rabc"
* StringUtils.chomp("\r") = ""
* StringUtils.chomp("\n") = ""
* StringUtils.chomp("\r\n") = ""
* </pre>
*
* @param str the String to chomp a newline from, may be null
* @return String without newline, <code>null</code> if null String input
*/
public static function chomp(str:String):String {
return chompString(str, '(\r\n|\r|\n)');
}
/**
* <p>Removes <code>separator</code> from the end of
* <code>str</code> if it's there, otherwise leave it alone.</p>
*
* <p>It now more closely matches Perl chomp.
* For the previous behavior, use #substringBeforeLast(String, String).
* This method uses #endsWith(String).</p>
*
* <pre>
* StringUtils.chompString(null, *) = null
* StringUtils.chompString("", *) = ""
* StringUtils.chompString("foobar", "bar") = "foo"
* StringUtils.chompString("foobar", "baz") = "foobar"
* StringUtils.chompString("foo", "foo") = ""
* StringUtils.chompString("foo ", "foo") = "foo "
* StringUtils.chompString(" foo", "foo") = " "
* StringUtils.chompString("foo", "foooo") = "foo"
* StringUtils.chompString("foo", "") = "foo"
* StringUtils.chompString("foo", null) = "foo"
* </pre>
*
* @param str the String to chomp from, may be null
* @param separator separator String, may be null
* @return String without trailing separator, <code>null</code> if null String input
*/
public static function chompString(str:String, separator:String):String {
if (isEmpty(str) || separator == null) {
return str;
}
return str.replace(new RegExp(separator + '$', ''), '')
}
/**
* <p>Removes control characters(char <= 32) from both
* ends of this String, handling <code>null</code> by returning
* <code>null</code>.</p>
*
* <p>Trim removes start and end characters <= 32.
* To strip whitespace use #strip(String).</p>
*
* <p>To trim your choice of characters, use the
* #strip(String, String) methods.</p>
*
* <pre>
* StringUtils.trim(null) = null
* StringUtils.trim("") = ""
* StringUtils.trim(" ") = ""
* StringUtils.trim("abc") = "abc"
* StringUtils.trim(" abc ") = "abc"
* </pre>
*
* @param str the String to be trimmed, may be null
* @return the trimmed string, <code>null</code> if null String input
*/
public static function trim(str:String):String {
if (str == null) {
return null;
}
return str.replace(/^\s*/, '').replace(/\s*$/, '');
}
/**
* <p>Deletes all 'space' characters from a String as defined by
*
* <pre>
* StringUtils.deleteSpaces(null) = null
* StringUtils.deleteSpaces("") = ""
* StringUtils.deleteSpaces("abc") = "abc"
* StringUtils.deleteSpaces(" \tabc \n ") = " abc "
* StringUtils.deleteSpaces("a\nb\tc ") = "abc "
* </pre>
*
* <p>Spaces are defined as <code>{'\t', '\r', '\n', '\b'}</code>
* in line with the deprecated <code>isSpace</code> method.</p>
*
* @param str the String to delete spaces from, may be null
* @return the String without 'spaces', <code>null</code> if null String input
*/
public static function deleteSpaces(str:String):String {
return deleteFromString(str, /\t|\r|\n|\b/g);
}
/**
* <p>Deletes all whitespaces from a String.</p>
*
* <pre>
* StringUtils.deleteWhitespace(null) = null
* StringUtils.deleteWhitespace("") = ""
* StringUtils.deleteWhitespace("abc") = "abc"
* StringUtils.deleteWhitespace(" ab c ") = "abc"
* </pre>
*
* @param str the String to delete whitespace from, may be null
* @return the String without whitespaces, <code>null</code> if null String input
*/
public static function deleteWhitespace(str:String):String {
return deleteFromString(str, /\s/g);
}
private static function deleteFromString(str:String, pattern:RegExp):String {
if (isEmpty(str)) {
return str;
}
return str.replace(pattern, '');
}
/**
* <p>Gets the leftmost <code>len</code> characters of a String.</p>
*
* <p>If <code>len</code> characters are not available, or the
* String is <code>null</code>, the String will be returned without
* an exception. An exception is thrown if len is negative.</p>
*
* <pre>
* StringUtils.left(null, *) = null
* StringUtils.left(*, -ve) = ""
* StringUtils.left("", *) = ""
* StringUtils.left("abc", 0) = ""
* StringUtils.left("abc", 2) = "ab"
* StringUtils.left("abc", 4) = "abc"
* </pre>
*
* @param str the String to get the leftmost characters from, may be null
* @param len the length of the required String, must be zero or positive
* @return the leftmost characters, <code>null</code> if null String input
*/
public static function left(str:String, len:int):String {
if (str == null) {
return null;
}
if (len < 0) {
return EMPTY;
}
if (str.length <= len) {
return str;
}
return str.substring(0, len);
}
/**
* <p>Centers a String in a larger String of size <code>size</code>.
* Uses a supplied String as the value to pad the String with.</p>
*
* <p>If the size is less than the String length, the String is returned.
* A <code>null</code> String returns <code>null</code>.
* A negative size is treated as zero.</p>
*
* <pre>
* StringUtils.center(null, *, *) = null
* StringUtils.center("", 4, " ") = " "
* StringUtils.center("ab", -1, " ") = "ab"
* StringUtils.center("ab", 4, " ") = " ab "
* StringUtils.center("abcd", 2, " ") = "abcd"
* StringUtils.center("a", 4, " ") = " a "
* StringUtils.center("a", 4, "yz") = "yayz"
* StringUtils.center("abc", 7, null) = " abc "
* StringUtils.center("abc", 7, "") = " abc "
* </pre>
*
* @param str the String to center, may be null
* @param size the int size of new String, negative treated as zero
* @param padStr the String to pad the new String with, must not be null or empty
* @return centered String, <code>null</code> if null String input
*/
public static function center(str:String, size:int, padStr:String):String {
if (str == null || size <= 0) {
return str;
}
if (isEmpty(padStr)) {
padStr = " ";
}
var strLen:int = str.length;
var pads:int = size - strLen;
if (pads <= 0) {
return str;
}
str = leftPad(str, strLen + pads / 2, padStr);
str = rightPad(str, size, padStr);
return str;
}
/**
* <p>Left pad a String with a specified String.</p>
*
* <p>Pad to a size of <code>size</code>.</p>
*
* <pre>
* StringUtils.leftPad(null, *, *) = null
* StringUtils.leftPad("", 3, "z") = "zzz"
* StringUtils.leftPad("bat", 3, "yz") = "bat"
* StringUtils.leftPad("bat", 5, "yz") = "yzbat"
* StringUtils.leftPad("bat", 8, "yz") = "yzyzybat"
* StringUtils.leftPad("bat", 1, "yz") = "bat"
* StringUtils.leftPad("bat", -1, "yz") = "bat"
* StringUtils.leftPad("bat", 5, null) = " bat"
* StringUtils.leftPad("bat", 5, "") = " bat"
* </pre>
*
* @param str the String to pad out, may be null
* @param size the size to pad to
* @param padStr the String to pad with, null or empty treated as single space
* @return left padded String or original String if no padding is necessary,
* <code>null</code> if null String input
*/
public static function leftPad(str:String, size:int, padStr:String):String {
if (str == null) {
return null;
}
if (isEmpty(padStr)) {
padStr = " ";
}
var padLen:int = padStr.length;
var strLen:int = str.length;
var pads:int = size - strLen;
if (pads <= 0) {
return str; // returns original String when possible
}
if (padLen == 1 && pads <= PAD_LIMIT) {
return leftPadChar(str, size, padStr.charAt(0));
}
if (pads == padLen) {
return padStr.concat(str);
} else if (pads < padLen) {
return padStr.substring(0, pads).concat(str);
} else {
var padding:Array = [];
var padChars:Array = padStr.split("");
for (var i:int = 0; i < pads; i++) {
padding[i] = padChars[i % padLen];
}
return padding.join("").concat(str);
}
}
/**
* <p>Left pad a String with a specified character.</p>
*
* <p>Pad to a size of <code>size</code>.</p>
*
* <pre>
* StringUtils.leftPadChar(null, *, *) = null
* StringUtils.leftPadChar("", 3, 'z') = "zzz"
* StringUtils.leftPadChar("bat", 3, 'z') = "bat"
* StringUtils.leftPadChar("bat", 5, 'z') = "zzbat"
* StringUtils.leftPadChar("bat", 1, 'z') = "bat"
* StringUtils.leftPadChar("bat", -1, 'z') = "bat"
* </pre>
*
* @param str the String to pad out, may be null
* @param size the size to pad to
* @param padChar the character to pad with
* @return left padded String or original String if no padding is necessary,
* <code>null</code> if null String input
*/
public static function leftPadChar(str:String, size:int, padChar:String):String {
if (str == null) {
return null;
}
var pads:int = size - str.length;
if (pads <= 0) {
return str; // returns original String when possible
}
if (pads > PAD_LIMIT) {
return leftPad(str, size, padChar);
}
return padding(pads, padChar).concat(str);
}
/**
* <p>Right pad a String with a specified String.</p>
*
* <p>The String is padded to the size of <code>size</code>.</p>
*
* <pre>
* StringUtils.rightPad(null, *, *) = null
* StringUtils.rightPad("", 3, "z") = "zzz"
* StringUtils.rightPad("bat", 3, "yz") = "bat"
* StringUtils.rightPad("bat", 5, "yz") = "batyz"
* StringUtils.rightPad("bat", 8, "yz") = "batyzyzy"
* StringUtils.rightPad("bat", 1, "yz") = "bat"
* StringUtils.rightPad("bat", -1, "yz") = "bat"
* StringUtils.rightPad("bat", 5, null) = "bat "
* StringUtils.rightPad("bat", 5, "") = "bat "
* </pre>
*
* @param str the String to pad out, may be null
* @param size the size to pad to
* @param padStr the String to pad with, null or empty treated as single space
* @return right padded String or original String if no padding is necessary,
* <code>null</code> if null String input
*/
public static function rightPad(str:String, size:int, padStr:String):String {
if (str == null) {
return null;
}
if (isEmpty(padStr)) {
padStr = " ";
}
var padLen:int = padStr.length;
var strLen:int = str.length;
var pads:int = size - strLen;
if (pads <= 0) {
return str; // returns original String when possible
}
if (padLen == 1 && pads <= PAD_LIMIT) {
return rightPadChar(str, size, padStr.charAt(0));
}
if (pads == padLen) {
return str.concat(padStr);
} else if (pads < padLen) {
return str.concat(padStr.substring(0, pads));
} else {
var padding:Array = [];
var padChars:Array = padStr.split("");
for (var i:int = 0; i < pads; i++) {
padding[i] = padChars[i % padLen];
}
return str.concat(padding.join(""));
}
}
/**
* <p>Right pad a String with a specified character.</p>
*
* <p>The String is padded to the size of <code>size</code>.</p>
*
* <pre>
* StringUtils.rightPadChar(null, *, *) = null
* StringUtils.rightPadChar("", 3, 'z') = "zzz"
* StringUtils.rightPadChar("bat", 3, 'z') = "bat"
* StringUtils.rightPadChar("bat", 5, 'z') = "batzz"
* StringUtils.rightPadChar("bat", 1, 'z') = "bat"
* StringUtils.rightPadChar("bat", -1, 'z') = "bat"
* </pre>
*
* @param str the String to pad out, may be null
* @param size the size to pad to
* @param padChar the character to pad with
* @return right padded String or original String if no padding is necessary,
* <code>null</code> if null String input
*/
public static function rightPadChar(str:String, size:int, padChar:String):String {
if (str == null) {
return null;
}
var pads:int = size - str.length;
if (pads <= 0) {
return str; // returns original String when possible
}
if (pads > PAD_LIMIT) {
return rightPad(str, size, padChar);
}
return str.concat(padding(pads, padChar));
}
/**
* <p>Returns padding using the specified delimiter repeated
* to a given length.</p>
*
* <pre>
* StringUtils.padding(0, 'e') = ""
* StringUtils.padding(3, 'e') = "eee"
* StringUtils.padding(-2, 'e') = IndexOutOfBoundsException
* </pre>
*
* @param repeat number of times to repeat delim
* @param padChar character to repeat
* @return String with repeated character
*/
private static function padding(repeat:int, padChar:String):String {
var buffer:String = '';
for (var i:int = 0; i < repeat; i++) {
buffer += padChar;
}
return buffer;
}
/**
* <p>Replaces all occurrences of a String within another String.</p>
*
* <p>A <code>null</code> reference passed to this method is a no-op.</p>
*
* <pre>
* StringUtils.replace(null, *, *) = null
* StringUtils.replace("", *, *) = ""
* StringUtils.replace("any", null, *) = "any"
* StringUtils.replace("any", *, null) = "any"
* StringUtils.replace("any", "", *) = "any"
* StringUtils.replace("aba", "a", null) = "aba"
* StringUtils.replace("aba", "a", "") = "b"
* StringUtils.replace("aba", "a", "z") = "zbz"
* </pre>
*
* @param text text to search and replace in, may be null
* @param pattern the String to search for, may be null
* @param repl the String to replace with, may be null
* @return the text with any replacements processed,
* <code>null</code> if null String input
*/
public static function replace(text:String, pattern:String, repl:String):String {
if (text == null || isEmpty(pattern) || repl == null) {
return text;
}
return text.replace(new RegExp(pattern, 'g'), repl);
}
/**
* <p>Replaces a String with another String inside a larger String,
* for the first <code>max</code> values of the search String.</p>
*
* <p>A <code>null</code> reference passed to this method is a no-op.</p>
*
* <pre>
* StringUtils.replaceTo(null, *, *, *) = null
* StringUtils.replaceTo("", *, *, *) = ""
* StringUtils.replaceTo("any", null, *, *) = "any"
* StringUtils.replaceTo("any", *, null, *) = "any"
* StringUtils.replaceTo("any", "", *, *) = "any"
* StringUtils.replaceTo("any", *, *, 0) = "any"
* StringUtils.replaceTo("abaa", "a", null, -1) = "abaa"
* StringUtils.replaceTo("abaa", "a", "", -1) = "b"
* StringUtils.replaceTo("abaa", "a", "z", 0) = "abaa"
* StringUtils.replaceTo("abaa", "a", "z", 1) = "zbaa"
* StringUtils.replaceTo("abaa", "a", "z", 2) = "zbza"
* StringUtils.replaceTo("abaa", "a", "z", -1) = "zbzz"
* </pre>
*
* @param text text to search and replace in, may be null
* @param repl the String to search for, may be null
* @param with the String to replace with, may be null
* @param max maximum number of values to replace, or <code>-1</code> if no maximum
* @return the text with any replacements processed,
* <code>null</code> if null String input
*/
public static function replaceTo(text:String, pattern:String, repl:String, max:int):String {
if (text == null || isEmpty(pattern) || repl == null || max == 0) {
return text;
}
var buf:String = "";
var start:int = 0;
var end:int = 0;
while ((end = text.indexOf(pattern, start)) != -1) {
buf += text.substring(start, end) + repl;
start = end + pattern.length;
if (--max == 0) {
break;
}
}
return buf += text.substring(start);
}
/**
* <p>Replaces a String with another String inside a larger String, once.</p>
*
* <p>A <code>null</code> reference passed to this method is a no-op.</p>
*
* <pre>
* StringUtils.replaceOnce(null, *, *) = null
* StringUtils.replaceOnce("", *, *) = ""
* StringUtils.replaceOnce("any", null, *) = "any"
* StringUtils.replaceOnce("any", *, null) = "any"
* StringUtils.replaceOnce("any", "", *) = "any"
* StringUtils.replaceOnce("aba", "a", null) = "aba"
* StringUtils.replaceOnce("aba", "a", "") = "ba"
* StringUtils.replaceOnce("aba", "a", "z") = "zba"
* </pre>
*
* @see #replaceTo(text:String, pattern:String, repl:String, max:int)
* @param text text to search and replace in, may be null
* @param repl the String to search for, may be null
* @param with the String to replace with, may be null
* @return the text with any replacements processed,
* <code>null</code> if null String input
*/
public static function replaceOnce(text:String, pattern:String, repl:String):String {
if (text == null || isEmpty(pattern) || repl == null) {
return text;
}
return text.replace(new RegExp(pattern, ''), repl);
}
/**
* <p>Returns either the passed in String, or if the String is
* empty or <code>null</code>, the value of <code>defaultStr</code>.</p>
*
* <pre>
* StringUtils.defaultIfEmpty(null, "NULL") = "NULL"
* StringUtils.defaultIfEmpty("", "NULL") = "NULL"
* StringUtils.defaultIfEmpty("bat", "NULL") = "bat"
* </pre>
*
* @param str the String to check, may be null
* @param defaultStr the default String to return
* if the input is empty("") or <code>null</code>, may be null
* @return the passed in String, or the default
*/
public static function defaultIfEmpty(str:String, defaultStr:String):String {
return isEmpty(str) ? defaultStr : str;
}
/**
* <p>Checks if a String is empty("") or null.</p>
*
* <pre>
* StringUtils.isEmpty(null) = true
* StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false
* StringUtils.isEmpty(" bob ") = false
* </pre>
*
* <p>NOTE: This method changed in Lang version 2.0.
* It no longer trims the String.
* That functionality is available in isBlank().</p>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is empty or null
*/
public static function isEmpty(str:String):Boolean {
if (str == null) {
return true;
}
return str.length == 0;
}
/**
* <p>Checks if a String is not empty("") and not null.</p>
*
* <pre>
* StringUtils.isNotEmpty(null) = false
* StringUtils.isNotEmpty("") = false
* StringUtils.isNotEmpty(" ") = true
* StringUtils.isNotEmpty("bob") = true
* StringUtils.isNotEmpty(" bob ") = true
* </pre>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is not empty and not null
*/
public static function isNotEmpty(str:String):Boolean {
return !isEmpty(str);
}
/**
* <p>Checks if a String is whitespace, empty("") or null.</p>
*
* <pre>
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("bob") = false
* StringUtils.isBlank(" bob ") = false
* </pre>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is null, empty or whitespace
*/
public static function isBlank(str:String):Boolean {
return isEmpty(trimToEmpty(str));
}
/**
* <p>Checks if a String is not empty(""), not <code>null</code>
* and not whitespace only.</p>
*
* <pre>
* StringUtils.isNotBlank(null) = false
* StringUtils.isNotBlank("") = false
* StringUtils.isNotBlank(" ") = false
* StringUtils.isNotBlank("bob") = true
* StringUtils.isNotBlank(" bob ") = true
* </pre>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is
* not empty and not null and not whitespace
*/
public static function isNotBlank(str:String):Boolean {
return !isBlank(str);
}
/**
* <p>Removes control characters (char <= 32) from both
* ends of this String returning <code>null</code> if the String is
* empty ("") after the trim or if it is <code>null</code>.
*
* <p>The String is trimmed using #trim().
* Trim removes start and end characters <= 32.</p>
*
* <pre>
* StringUtils.trimToNull(null) = null
* StringUtils.trimToNull("") = null
* StringUtils.trimToNull(" ") = null
* StringUtils.trimToNull("abc") = "abc"
* StringUtils.trimToNull(" abc ") = "abc"
* </pre>
*
* @param str the String to be trimmed, may be null
* @return the trimmed String,
* <code>null</code> if only chars <= 32, empty or null String input
*/
public static function trimToNull(str:String):String {
var ts:String = trim(str);
return isEmpty(ts) ? null : ts;
}
/**
* <p>Removes control characters (char <= 32) from both
* ends of this String returning an empty String ("") if the String
* is empty ("") after the trim or if it is <code>null</code>.
*
* <p>The String is trimmed using #trim().
* Trim removes start and end characters <= 32.
* To strip whitespace use #stripToEmpty(String).</p>
*
* <pre>
* StringUtils.trimToEmpty(null) = ""
* StringUtils.trimToEmpty("") = ""
* StringUtils.trimToEmpty(" ") = ""
* StringUtils.trimToEmpty("abc") = "abc"
* StringUtils.trimToEmpty(" abc ") = "abc"
* </pre>
*
* @param str the String to be trimmed, may be null
* @return the trimmed String, or an empty String if <code>null</code> input
*/
public static function trimToEmpty(str:String):String {
return str == null ? EMPTY : trim(str);
}
/**
* <p>Capitalizes a String changing the first letter to title case.
* No other letters are changed.</p>
*
* A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* StringUtils.capitalize(null) = null
* StringUtils.capitalize("") = ""
* StringUtils.capitalize("cat") = "Cat"
* StringUtils.capitalize("cAt") = "CAt"
* </pre>
*
* @param str the String to capitalize, may be null
* @return the capitalized String, <code>null</code> if null String input
* @see titleize(String)
* @see #uncapitalize(String)
*/
public static function capitalize(str:String):String {
if (isEmpty(str)) {
return str;
}
return str.charAt(0).toUpperCase() + str.substring(1);
}
/**
* <p>Uncapitalizes a String changing the first letter to title case.
* No other letters are changed.</p>
*
* <pre>
* StringUtils.uncapitalize(null) = null
* StringUtils.uncapitalize("") = ""
* StringUtils.uncapitalize("Cat") = "cat"
* StringUtils.uncapitalize("CAT") = "cAT"
* </pre>
*
* @param str the String to uncapitalize, may be null
* @return the uncapitalized String, <code>null</code> if null String input
* @see #capitalize(String)
*/
public static function uncapitalize(str:String):String {
if (isEmpty(str)) {
return str;
}
return str.charAt(0).toLowerCase() + str.substring(1);
}
/**
* <p>Capitalizes all the words and replaces some characters in
* the string to create a nicer looking title.
* Titleize is meant for creating pretty output.</p>
*
* <pre>
* StringUtils.titleize(null) = null
* StringUtils.titleize("") = ""
* StringUtils.titleize("man from the boondocks") = "Man From The Boondocks"
* StringUtils.titleize("man from THE bOOndocks") = "Man From The Boondocks"
* </pre>
*
* @param str the String to uncapitalize, may be null
* @return the uncapitalized String, <code>null</code> if null String input
* @see #capitalize(String)
*/
public static function titleize(str:String):String {
if (isEmpty(str)) {
return str;
}
var words:Array = str.toLowerCase().split(' ');
for (var i:int = 0; i < words.length; i++) {
words[i] = capitalize(words[i]);
}
return words.join(' ');
}
/**
* <p>Gets the substring after the first occurrence of a separator.
* The separator is not returned.</p>
*
* <p>A <code>null</code> string input will return <code>null</code>.
* An empty("") string input will return the empty string.
* A <code>null</code> separator will return the empty string if the
* input string is not <code>null</code>.</p>
*
* <pre>
* StringUtils.substringAfter(null, *) = null
* StringUtils.substringAfter("", *) = ""
* StringUtils.substringAfter(*, null) = ""
* StringUtils.substringAfter("abc", "a") = "bc"
* StringUtils.substringAfter("abcba", "b") = "cba"
* StringUtils.substringAfter("abc", "c") = ""
* StringUtils.substringAfter("abc", "d") = ""
* StringUtils.substringAfter("abc", "") = "abc"
* </pre>
*
* @param str the String to get a substring from, may be null
* @param separator the String to search for, may be null
* @return the substring after the first occurrence of the separator,
* <code>null</code> if null String input
*/
public static function substringAfter(str:String, separator:String):String {
if (isEmpty(str)) {
return str;
}
if (separator == null) {
return EMPTY;
}
var pos:int = str.indexOf(separator);
if (pos == INDEX_NOT_FOUND) {
return EMPTY;
}
return str.substring(pos + separator.length);
}
/**
* <p>Gets the substring after the last occurrence of a separator.
* The separator is not returned.</p>
*
* <p>A <code>null</code> string input will return <code>null</code>.
* An empty("") string input will return the empty string.
* An empty or <code>null</code> separator will return the empty string if
* the input string is not <code>null</code>.</p>
*
* <pre>
* StringUtils.substringAfterLast(null, *) = null
* StringUtils.substringAfterLast("", *) = ""
* StringUtils.substringAfterLast(*, "") = ""
* StringUtils.substringAfterLast(*, null) = ""
* StringUtils.substringAfterLast("abc", "a") = "bc"
* StringUtils.substringAfterLast("abcba", "b") = "a"
* StringUtils.substringAfterLast("abc", "c") = ""
* StringUtils.substringAfterLast("a", "a") = ""
* StringUtils.substringAfterLast("a", "z") = ""
* </pre>
*
* @param str the String to get a substring from, may be null
* @param separator the String to search for, may be null
* @return the substring after the last occurrence of the separator,
* <code>null</code> if null String input
*/
public static function substringAfterLast(str:String, separator:String):String {
if (isEmpty(str)) {
return str;
}
if (isEmpty(separator)) {
return EMPTY;
}
var pos:int = str.lastIndexOf(separator);
if (pos == INDEX_NOT_FOUND || pos == (str.length - separator.length)) {
return EMPTY;
}
return str.substring(pos + separator.length);
}
/**
* <p>Gets the substring before the first occurrence of a separator.
* The separator is not returned.</p>
*
* <p>A <code>null</code> string input will return <code>null</code>.
* An empty("") string input will return the empty string.
* A <code>null</code> separator will return the input string.</p>
*
* <pre>
* StringUtils.substringBefore(null, *) = null
* StringUtils.substringBefore("", *) = ""
* StringUtils.substringBefore("abc", "a") = ""
* StringUtils.substringBefore("abcba", "b") = "a"
* StringUtils.substringBefore("abc", "c") = "ab"
* StringUtils.substringBefore("abc", "d") = "abc"
* StringUtils.substringBefore("abc", "") = ""
* StringUtils.substringBefore("abc", null) = "abc"
* </pre>
*
* @param str the String to get a substring from, may be null
* @param separator the String to search for, may be null
* @return the substring before the first occurrence of the separator,
* <code>null</code> if null String input
*/
public static function substringBefore(str:String, separator:String):String {
if (isEmpty(str) || separator == null) {
return str;
}
if (separator.length == 0) {
return EMPTY;
}
var pos:int = str.indexOf(separator);
if (pos == INDEX_NOT_FOUND) {
return str;
}
return str.substring(0, pos);
}
/**
* <p>Gets the substring before the last occurrence of a separator.
* The separator is not returned.</p>
*
* <p>A <code>null</code> string input will return <code>null</code>.
* An empty("") string input will return the empty string.
* An empty or <code>null</code> separator will return the input string.</p>
*
* <pre>
* StringUtils.substringBeforeLast(null, *) = null
* StringUtils.substringBeforeLast("", *) = ""
* StringUtils.substringBeforeLast("abcba", "b") = "abc"
* StringUtils.substringBeforeLast("abc", "c") = "ab"
* StringUtils.substringBeforeLast("a", "a") = ""
* StringUtils.substringBeforeLast("a", "z") = "a"
* StringUtils.substringBeforeLast("a", null) = "a"
* StringUtils.substringBeforeLast("a", "") = "a"
* </pre>
*
* @param str the String to get a substring from, may be null
* @param separator the String to search for, may be null
* @return the substring before the last occurrence of the separator,
* <code>null</code> if null String input
*/
public static function substringBeforeLast(str:String, separator:String):String {
if (isEmpty(str) || isEmpty(separator)) {
return str;
}
var pos:int = str.lastIndexOf(separator);
if (pos == INDEX_NOT_FOUND) {
return str;
}
return str.substring(0, pos);
}
/**
* <p>Gets the String that is nested in between two Strings.
* Only the first match is returned.</p>
*
* <p>A <code>null</code> input String returns <code>null</code>.
* A <code>null</code> open/close returns <code>null</code>(no match).
* An empty("") open/close returns an empty string.</p>
*
* <pre>
* StringUtils.substringBetween(null, *, *) = null
* StringUtils.substringBetween("", "", "") = ""
* StringUtils.substringBetween("", "", "tag") = null
* StringUtils.substringBetween("", "tag", "tag") = null
* StringUtils.substringBetween("yabcz", null, null) = null
* StringUtils.substringBetween("yabcz", "", "") = ""
* StringUtils.substringBetween("yabcz", "y", "z") = "abc"
* StringUtils.substringBetween("yabczyabcz", "y", "z") = "abc"
* </pre>
*
* @param str the String containing the substring, may be null