-
Notifications
You must be signed in to change notification settings - Fork 7
/
StringUtils.cls
1922 lines (1710 loc) · 60.7 KB
/
StringUtils.cls
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
/* ============================================================
* This code is part of the "apex-lang" open source project avaiable at:
*
* http://code.google.com/p/apex-lang/
*
* This code is licensed under the Apache License, Version 2.0. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
* ============================================================
*/
/**
* Port of the StringUtils class from Java to Apex. This
* class is part of the Apache Commons Lang project available
* at http://commons.apache.org/lang/.
*/
global class StringUtils {
global static final String EMPTY = '';
global static final String LF = '\n';
global static final String CR = '\r';
global static final Integer INDEX_NOT_FOUND = -1;
private static final Integer PAD_LIMIT = 8192;
global static String abbreviate(String str, Integer maxWidth) {
return abbreviate(str, 0, maxWidth);
}
global static String abbreviate(String str, Integer offset, Integer maxWidth) {
if (str == null) {
return null;
}
if (maxWidth < 4) {
throw new IllegalArgumentException('Minimum abbreviation width is 4');
}
if (str.length() <= maxWidth) {
return str;
}
if (offset > str.length()) {
offset = str.length();
}
if ((str.length() - offset) < (maxWidth - 3)) {
offset = str.length() - (maxWidth - 3);
}
if (offset <= 4) {
return str.substring(0, maxWidth - 3) + '...';
}
if (maxWidth < 7) {
throw new IllegalArgumentException('Minimum abbreviation width with offset is 7');
}
if ((offset + (maxWidth - 3)) < str.length()) {
return '...' + abbreviate(str.substring(offset), maxWidth - 3);
}
return '...' + str.substring(str.length() - (maxWidth - 3));
}
global static String capitalize(String str) {
if(isBlank(str)){
return str;
}
return upperCase(str.substring(0,1)) + str.substring(1);
}
global static String center(String str, Integer size) {
return center(str, size, ' ');
}
global static String center(String str, Integer size, String padStr) {
if (str == null || size <= 0) {
return str;
}
if (isEmpty(padStr)) {
padStr = ' ';
}
Integer strLen = str.length();
Integer padCharCount = size - strLen;
if (padCharCount <= 0) {
return str;
}
str = leftPad(str, strLen + padCharCount / 2, padStr);
str = rightPad(str, size, padStr);
return str;
}
global static String charAt(String str, Integer index) {
if(str == null){
return null;
}
if(str.length() <= 0){
return str;
}
if(index < 0 || index >= str.length()){
return null;
}
return str.substring(index, index+1);
}
global static String chomp(String str) {
if (isEmpty(str)) {
return str;
}
if (str.length() == 1) {
String ch = charAt(str,0);
if (ch == CR || ch == LF) {
return EMPTY;
}
return str;
}
Integer lastIdx = str.length() - 1;
String last = charAt(str, lastIdx);
if (LF.equals(last)) {
if (CR.equals(charAt(str,lastIdx - 1))) {
lastIdx--;
}
} else if (!CR.equals(last)) {
lastIdx++;
}
return str.substring(0, lastIdx);
}
global static String chomp(String str, String separator) {
if (isEmpty(str) || separator == null) {
return str;
}
if (str.endsWith(separator)) {
return str.substring(0, str.length() - separator.length());
}
return str;
}
global static String chop(String str) {
if (str == null) {
return null;
}
Integer strLen = str.length();
if (strLen < 2) {
return EMPTY;
}
Integer lastIdx = strLen - 1;
String ret = str.substring(0, lastIdx);
String last = charAt(str, lastIdx);
if (LF.equals(last)) {
if (CR.equals(charAt(ret,lastIdx - 1))) {
return ret.substring(0, lastIdx - 1);
}
}
return ret;
}
global static Boolean contains(String str, String searchStr) {
if (str == null || searchStr == null) {
return false;
}
return str.indexOf(searchStr,0) >= 0;
}
global static boolean containsAny(String str, String searchChars) {
if (str == null || str.length() == 0 || searchChars == null || searchChars.length() == 0) {
return false;
}
for (Integer i = 0; i < str.length(); i++) {
if(searchChars.contains(charAt(str, i))){
return true;
}
}
return false;
}
global static Boolean containsIgnoreCase(String str, String searchStr) {
if (str == null || searchStr == null) {
return false;
}
return contains(str.toUpperCase(), searchStr.toUpperCase());
}
global static boolean containsNone(String str, String invalidChars) {
if (str == null || invalidChars == null) {
return true;
}
Integer strSize = str.length();
Integer invalidSize = invalidChars.length();
for (Integer i = 0; i < strSize; i++) {
String ch = charAt(str, i);
if(invalidChars.contains(ch)){
return false;
}
}
return true;
}
global static boolean containsOnly(String str, String valid) {
if ((valid == null) || (str == null)) {
return false;
}
if (str.length() == 0) {
return true;
}
if (valid.length() == 0) {
return false;
}
return indexOfAnyBut(str, valid) == -1;
}
global static Integer countMatches(String str, String sub) {
if (isEmpty(str) || isEmpty(sub)) {
return 0;
}
Integer count = 0;
Integer idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1) {
count++;
idx += sub.length();
}
return count;
}
global static String defaultIfEmpty(String str, String defaultStr) {
return StringUtils.isEmpty(str) ? defaultStr : str;
}
global static String defaultString(String str) {
return str == null ? EMPTY : str;
}
global static String defaultString(String str, String defaultStr) {
return str == null ? defaultStr : str;
}
global static String deleteWhitespace(String str) {
if (isEmpty(str)) {
return str;
}
String returnString = '';
String currentChar = null;
for (Integer i = 0; i < str.length(); i++) {
currentChar = charAt(str,i);
if (!Character.isWhitespace(currentChar)) {
returnString += currentChar;
}
}
return returnString;
}
global static String difference(String str1, String str2) {
//'abc',''
if (str1 == null) {
return str2;
}
if (str2 == null) {
return str1;
}
Integer at = indexOfDifference(str1, str2);
if (at == -1) {
return EMPTY;
}
if(at == 0){
return str2;
}
return str2.substring(at);
}
global static Integer indexOfDifference(String str1, String str2) {
if (str1 == str2) {
return -1;
}
if (str1 == null || str2 == null) {
return 0;
}
Integer i;
for (i = 0; i < str1.length() && i < str2.length(); ++i) {
if (charAt(str1,i) != charAt(str2,i)) {
break;
}
}
//if (i < str2.length() || i < str1.length()) {
return i;
//}
//return -1;
}
global static Integer indexOfDifference(String[] strs) {
if (strs == null || strs.size() <= 1) {
return -1;
}
Boolean anyStringNull = false;
Boolean allStringsNull = true;
Integer listSize = strs.size();
Integer shortestStrLen = NumberUtils.MAX_INTEGER;
Integer longestStrLen = 0;
// find the min and max string lengths; this avoids checking to make
// sure we are not exceeding the length of the string each time through
// the bottom loop.
for (Integer i = 0; i < listSize; i++) {
if (strs.get(i) == null) {
anyStringNull = true;
shortestStrLen = 0;
} else {
allStringsNull = false;
shortestStrLen = Math.min(strs.get(i).length(), shortestStrLen);
longestStrLen = Math.max(strs.get(i).length(), longestStrLen);
}
}
// handle lists containing all nulls or all empty strings
if (allStringsNull || (longestStrLen == 0 && !anyStringNull)) {
return -1;
}
// handle lists containing some nulls or some empty strings
if (shortestStrLen == 0) {
return 0;
}
// find the position with the first difference across all strings
Integer firstDiff = -1;
for (Integer stringPos = 0; stringPos < shortestStrLen; stringPos++) {
String comparisonChar = charAt(strs.get(0), stringPos);
for (Integer arrayPos = 1; arrayPos < listSize; arrayPos++) {
if (charAt(strs.get(arrayPos),stringPos) != comparisonChar) {
firstDiff = stringPos;
break;
}
}
if (firstDiff != -1) {
break;
}
}
if (firstDiff == -1 && shortestStrLen != longestStrLen) {
// we compared all of the characters up to the length of the
// shortest string and didn't find a match, but the string lengths
// vary, so return the length of the shortest string.
return shortestStrLen;
}
return firstDiff;
}
global static Boolean equals(String str1, String str2) {
return str1 == null ? str2 == null : str1.equals(str2);
}
global static Boolean equalsIgnoreCase(String str1, String str2) {
return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2);
}
global static Integer indexOf(String str, String searchStr) {
if (str == null || searchStr == null) {
return -1;
}
return str.indexOf(searchStr);
}
global static Integer indexOf(String str, String searchStr, Integer startPos) {
if (str == null || searchStr == null) {
return -1;
}
if (searchStr.length() == 0 && startPos >= str.length()) {
return str.length();
}
return str.indexOf(searchStr, startPos);
}
global static Integer ordinalIndexOf(String str, String searchStr, Integer ordinal) {
//'','',-1
if (str == null || searchStr == null || ordinal <= 0) {
return INDEX_NOT_FOUND;
}
if (searchStr.length() == 0) {
return 0;
}
Integer found = 0;
Integer index = INDEX_NOT_FOUND;
do {
index = str.indexOf(searchStr, index + 1);
if (index < 0) {
return index;
}
found++;
} while (found < ordinal);
return index;
}
global static String getCommonPrefix(String[] strs) {
if (strs == null || strs.size() == 0) {
return EMPTY;
}
Integer smallestIndexOfDiff = indexOfDifference(strs);
if (smallestIndexOfDiff == -1) {
// all strings were identical
if (strs.get(0) == null) {
return EMPTY;
}
return strs.get(0);
} else if (smallestIndexOfDiff == 0) {
// there were no common initial characters
return EMPTY;
} else {
// we found a common initial character sequence
return strs.get(0).substring(0, smallestIndexOfDiff);
}
}
global static Integer getLevenshteinDistance(String s, String t) {
if (s == null || t == null) {
throw new IllegalArgumentException('Strings must not be null');
}
/*
The difference between this impl. and the previous is that, rather
than creating and retaining a matrix of size s.length()+1 by t.length()+1,
we maintain two single-dimensional arrays of length s.length()+1. The first, d,
is the 'current working' distance array that maintains the newest distance cost
counts as we iterate through the characters of String s. Each time we increment
the index of String t we are comparing, d is copied to p, the second int[]. Doing so
allows us to retain the previous cost counts as required by the algorithm (taking
the minimum of the cost count to the left, up one, and diagonally up and to the left
of the current cost count being calculated). (Note that the arrays aren't really
copied anymore, just switched...this is clearly much better than cloning an array
or doing a System.arraycopy() each time through the outer loop.)
Effectively, the difference between the two implementations is this one does not
cause an out of memory condition when calculating the LD over two very large strings.
*/
Integer n = s.length(); // length of s
Integer m = t.length(); // length of t
if (n == 0) {
return m;
} else if (m == 0) {
return n;
}
if (n > m) {
// swap the input strings to consume less memory
String tmp = s;
s = t;
t = tmp;
n = m;
m = t.length();
}
Integer[] p = new Integer[n+1]; //'previous' cost array, horizontally
Integer[] d = new Integer[n+1]; // cost array, horizontally
Integer[] dSwap; //placeholder to assist in swapping p and d
// indexes into strings s and t
Integer i; // iterates through s
Integer j; // iterates through t
String t_j; // jth character of t
Integer cost; // cost
for (i = 0; i<=n; i++) {
p[i] = i;
}
for (j = 1; j<=m; j++) {
t_j = charAt(t,j-1);
d[0] = j;
for (i=1; i<=n; i++) {
cost = charAt(s,i-1)==t_j ? 0 : 1;
// minimum of cell to the left+1, to the top+1, diagonally left and up +cost
d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1), p[i-1]+cost);
}
// copy current distance counts to 'previous row' distance counts
dSwap = p;
p = d;
d = dSwap;
}
// our last action in the above loop was to switch d and p, so p now
// actually has the most recent cost counts
return p[n];
}
global static Integer indexOfAnyBut(String str, String searchChars) {
if (isEmpty(str) || searchChars == null) {
return -1;
}
for (Integer i = 0; i < str.length(); i++) {
if(searchChars.contains(charAt(str,i))){
continue;
}
return i;
}
return -1;
}
global static Boolean endsWith(String str, String suffix) {
return endsWith(str, suffix, false);
}
private static Boolean endsWith(String str, String suffix, Boolean ignoreCase) {
if (str == null || suffix == null) {
return (str == null && suffix == null);
}
if (suffix.length() > str.length()) {
return false;
}
Integer strOffset = str.length() - suffix.length();
String ending = str.substring(strOffset, str.length());
if(ignoreCase){
return suffix.equalsIgnoreCase(ending);
}
return suffix.equals(ending);
}
global static Boolean endsWithIgnoreCase(String str, String suffix) {
return endsWith(str, suffix, true);
}
global static Integer indexOfAny(String str, String searchChars) {
if (isEmpty(str) || searchChars == null) {
return -1;
}
for (Integer i = 0; i < str.length(); i++) {
String ch = charAt(str,i);
for (Integer j = 0; j < searchChars.length(); j++) {
if (charAt(searchChars,j) == ch) {
return i;
}
}
}
return -1;
}
global static Integer indexOfAny(String str, String[] searchStrs) {
if (str == null || searchStrs == null || searchStrs.size() == 0) {
return -1;
}
Integer returnIndexOf = NumberUtils.MAX_INTEGER;
Integer currentIndexOf = 0;
for(String searchStr : searchStrs){
if (searchStr == null) {
continue;
}
currentIndexOf = str.indexOf(searchStr);
if (currentIndexOf == -1) {
continue;
}
if (currentIndexOf < returnIndexOf) {
returnIndexOf = currentIndexOf;
}
}
return (returnIndexOf == NumberUtils.MAX_INTEGER) ? -1 : returnIndexOf;
}
global static boolean isAlpha(String str) {
if (str == null) {
return false;
}
Integer size = str.length();
for (Integer i = 0; i < size; i++) {
if (Character.isLetter(charAt(str,i)) == false) {
return false;
}
}
return true;
}
global static boolean isAlphaSpace(String str) {
if (str == null) {
return false;
}
Integer sz = str.length();
for (Integer i = 0; i < sz; i++) {
if ((Character.isLetter(charAt(str,i)) == false) && (charAt(str,i) != ' ')) {
return false;
}
}
return true;
}
global static boolean isAlphanumeric(String str) {
if (str == null) {
return false;
}
Integer sz = str.length();
for (Integer i = 0; i < sz; i++) {
if (Character.isLetterOrDigit(charAt(str,i)) == false) {
return false;
}
}
return true;
}
global static boolean isAlphanumericSpace(String str) {
if (str == null) {
return false;
}
Integer sz = str.length();
for (Integer i = 0; i < sz; i++) {
if ((Character.isLetterOrDigit(charAt(str,i)) == false) && (charAt(str,i) != ' ')) {
return false;
}
}
return true;
}
global static boolean isAsciiPrintable(String str) {
if (str == null) {
return false;
}
Integer sz = str.length();
for (Integer i = 0; i < sz; i++) {
if (Character.isAsciiPrintable(charAt(str,i)) == false) {
return false;
}
}
return true;
}
global static boolean isNumeric(String str) {
if (str == null) {
return false;
}
Integer sz = str.length();
for (Integer i = 0; i < sz; i++) {
if (Character.isDigit(charAt(str,i)) == false) {
return false;
}
}
return true;
}
global static boolean isNumericSpace(String str) {
if (str == null) {
return false;
}
Integer sz = str.length();
for (Integer i = 0; i < sz; i++) {
if ((Character.isDigit(charAt(str,i)) == false) && (charAt(str,i) != ' ')) {
return false;
}
}
return true;
}
global static boolean isWhitespace(String str) {
if (str == null) {
return false;
}
Integer sz = str.length();
for (Integer i = 0; i < sz; i++) {
if ((Character.isWhitespace(charAt(str,i)) == false)) {
return false;
}
}
return true;
}
global static boolean isBlank(String str) {
return str == null || str.trim() == null || str.trim().length() == 0;
}
global static boolean isNotBlank(String str) {
return !isBlank(str);
}
global static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
global static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
global static String joinArray(Object[] objectArray) {
return joinArray(objectArray, null);
}
global static String joinArray(Object[] objectArray, String separator) {
if (objectArray == null) {
return null;
}
return joinArray(objectArray, separator, 0, objectArray.size());
}
global static String joinArray(Object[] objectArray, String separator, Integer startIndex, Integer endIndex) {
if (objectArray == null) {
return null;
}
if (separator == null) {
separator = EMPTY;
}
String buf = '';
if(startIndex < 0){
startIndex = 0;
}
Boolean isFirst = true;
for (Integer i = startIndex; i < endIndex && i < objectArray.size(); i++) {
if(objectArray[i] != null) {
if(isFirst){
isFirst = false;
} else {
buf += separator;
}
buf += objectArray[i];
}
}
return buf;
}
global static String joinStrings(Set<String> strings, String separator){
return joinSet(strings,separator);
}
global static String joinSet(Set<Blob> blobSet, String separator){
return joinSet(SetUtils.blobToObject(blobSet),separator);
}
global static String joinSet(Set<Boolean> booleanSet, String separator){
return joinSet(SetUtils.booleanToObject(booleanSet),separator);
}
global static String joinSet(Set<Date> dateSet, String separator){
return joinSet(SetUtils.dateToObject(dateSet),separator);
}
global static String joinSet(Set<Datetime> datetimeSet, String separator){
return joinSet(SetUtils.datetimeToObject(datetimeSet),separator);
}
global static String joinSet(Set<Decimal> decimalSet, String separator){
return joinSet(SetUtils.decimalToObject(decimalSet),separator);
}
global static String joinSet(Set<Double> doubleSet, String separator){
return joinSet(SetUtils.doubleToObject(doubleSet),separator);
}
global static String joinSet(Set<ID> idSet, String separator){
return joinSet(SetUtils.idToObject(idSet),separator);
}
global static String joinSet(Set<Integer> integerSet, String separator){
return joinSet(SetUtils.integerToObject(integerSet),separator);
}
global static String joinSet(Set<Long> longSet, String separator){
return joinSet(SetUtils.longToObject(longSet),separator);
}
global static String joinSet(Set<Time> timeSet, String separator){
return joinSet(SetUtils.timeToObject(timeSet),separator);
}
global static String joinSet(Set<String> stringSet, String separator){
return joinSet(SetUtils.stringToObject(stringSet),separator);
}
global static String joinSet(Set<Object> objectSet, String separator){
if(objectSet == null || objectSet.size() == 0){
return null;
}
Boolean isFirst = true;
String returnString = '';
for(Object anObject : objectSet){
if(StringUtils.isBlank(''+anObject)){
continue;
}
if(isFirst){
isFirst = false;
}else{
if(separator != null){
returnString += separator;
}
}
if(anObject instanceof Blob){ returnString += '' + ((Blob)anObject).toString() + '';
} else if(anObject instanceof Boolean){ returnString += ((Boolean)anObject);
} else if(anObject instanceof Date){ returnString += '' + ((Date)anObject) + '';
} else if(anObject instanceof Datetime){ returnString += '' + ((Datetime)anObject) + '';
} else if(anObject instanceof Integer){ returnString += ((Integer)anObject);
} else if(anObject instanceof Long){ returnString += ((Long)anObject);
} else if(anObject instanceof Decimal){ returnString += ((Decimal)anObject);
//} else if(anObject instanceof Double){ returnString += ((Double)anObject);
} else if(anObject instanceof String){ returnString += '' + ((String)anObject) + '';
//} else if(anObject instanceof ID){ returnString += '' + ((ID)anObject) + '';
} else if(anObject instanceof Time){ returnString += '' + ((Time)anObject) + '';}
}
return returnString;
}
global static String joinStrings(List<String> strings, String separator){
if(strings == null || strings.size() == 0){
return null;
}
Set<String> setToJoin = new Set<String>();
for(String value : strings){
setToJoin.add(value);
}
return joinStrings(setToJoin,separator);
}
global static Integer lastIndexOf(String str, String searchStr) {
if (str == null || searchStr == null) {
return -1;
}
return str.lastIndexOf(searchStr);
}
global static Integer lastIndexOf(String str, String searchStr, Integer startPos) {
if (str == null || searchStr == null || startPos < 0) {
return -1;
}
return lastIndexOf(substring(str,0,startPos+1), searchStr);
}
global static Integer lastIndexOfAny(String str, String[] searchStrs) {
if ((str == null) || (searchStrs == null)) {
return -1;
}
Integer sz = searchStrs.size();
Integer ret = -1;
Integer tmp = 0;
for (Integer i = 0; i < sz; i++) {
String searchStr = searchStrs[i];
if (searchStr == null) {
continue;
}
tmp = str.lastIndexOf(searchStr);
if (tmp > ret) {
ret = tmp;
}
}
return ret;
}
global static String left(String str, Integer len) {
if (str == null) {
return null;
}
if (len < 0) {
return EMPTY;
}
if (str.length() <= len) {
return str;
}
return str.substring(0, len);
}
global static String mid(String str, Integer pos, Integer len) {
if (str == null) {
return null;
}
if (len < 0 || pos >= str.length()) {
return EMPTY;
}
if (pos < 0) {
pos = 0;
}
if (str.length() <= (pos + len)) {
return str.substring(pos);
}
return str.substring(pos, pos + len);
}
global static String overlay(String str, String overlay, Integer startIndex, Integer endIndex) {
//'', 'abc', 0, 0
if (str == null) {
return null;
}
if (overlay == null) {
overlay = EMPTY;
}
Integer len = str.length();
if (startIndex < 0) {
startIndex = 0;
}
if (startIndex > len) {
startIndex = len;
}
if (endIndex < 0) {
endIndex = 0;
}
if (endIndex > len) {
endIndex = len;
}
if (startIndex > endIndex) {
Integer temp = startIndex;
startIndex = endIndex;
endIndex = temp;
}
return new StringBuffer()
.append(substring(str, 0, startIndex))
.append(overlay)
.append(substring(str, endIndex))
.toStr();
}
global static String leftPad(String str, Integer size) {
return leftPad(str, size, ' ');
}
global static String leftPad(String str, Integer size, String padStr) {
if (str == null) {
return null;
}
if (isEmpty(padStr)) {
padStr = ' ';
}
Integer padLen = padStr.length();
Integer strLen = str.length();
Integer padCharCount = size - strLen;
if (padCharCount <= 0) {
return str;
}
if (padCharCount == padLen) {
return padStr + str;
} else if (padCharCount < padLen) {
return padStr.substring(0, padCharCount) + str;
} else {
String padding = '';
for (Integer i = 0; i < padCharCount; i++) {
padding += padStr.substring(Math.mod(i,padLen),Math.mod(i,padLen)+1);
}
return padding + str;
}
}
global static Integer length(String str) {
return str == null ? 0 : str.length();
}
global static String lowerCase(String str) {
if(str == null){
return null;
}
return str.toLowerCase();
}
global static String remove(String str, String remove) {
if (isEmpty(str) || isEmpty(remove)) {
return str;
}
return replace(str, remove, EMPTY, -1);
}
global static String removeStart(String str, String remove) {
if (isEmpty(str) || isEmpty(remove)) {
return str;
}
if (str.startsWith(remove)){
if(remove.length() >= str.length()){
return '';
} else {
return str.substring(remove.length());
}
}
return str;
}
global static String removeStartIgnoreCase(String str, String remove) {
if (isEmpty(str) || isEmpty(remove)) {
return str;
}
if (startsWithIgnoreCase(str, remove)) {
return str.substring(remove.length());
}
return str;
}
global static String removeEnd(String str, String remove) {
if (isEmpty(str) || isEmpty(remove)) {
return str;
}
if (str.endsWith(remove)) {
return str.substring(0, str.length() - remove.length());
}
return str;
}
global static String removeEndIgnoreCase(String str, String remove) {
if (isEmpty(str) || isEmpty(remove)) {
return str;
}
if (endsWithIgnoreCase(str, remove)) {
return str.substring(0, str.length() - remove.length());
}
return str;
}
global static String repeat(String str, Integer repeat) {
if (str == null) {
return null;
}