-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path131.java
More file actions
2851 lines (2528 loc) · 71.4 KB
/
Copy path131.java
File metadata and controls
2851 lines (2528 loc) · 71.4 KB
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) 2003-present, Jodd Team (http://jodd.org)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
package jodd.util;
import jodd.core.JoddCore;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;
import java.util.function.Function;
import static jodd.util.StringPool.EMPTY;
/**
* Various String utilities.
* For even more String utilities, see {@link Format}.
*/
public class StringUtil {
// ---------------------------------------------------------------- replace
/**
* Replaces all occurrences of a certain pattern in a string with a
* replacement string. This is the fastest replace function known to author.
*
* @param s string to be inspected
* @param sub string pattern to be replaced
* @param with string that should go where the pattern was
*/
public static String replace(final String s, final String sub, final String with) {
if (sub.isEmpty()) {
return s;
}
int c = 0;
int i = s.indexOf(sub, c);
if (i == -1) {
return s;
}
int length = s.length();
StringBuilder sb = new StringBuilder(length + with.length());
do {
sb.append(s, c, i);
sb.append(with);
c = i + sub.length();
} while ((i = s.indexOf(sub, c)) != -1);
if (c < length) {
sb.append(s, c, length);
}
return sb.toString();
}
/**
* Replaces all occurrences of a character in a string.
*
* @param s input string
* @param sub character to replace
* @param with character to replace with
*/
public static String replaceChar(final String s, final char sub, final char with) {
int startIndex = s.indexOf(sub);
if (startIndex == -1) {
return s;
}
char[] str = s.toCharArray();
for (int i = startIndex; i < str.length; i++) {
if (str[i] == sub) {
str[i] = with;
}
}
return new String(str);
}
/**
* Replaces all occurrences of a characters in a string.
*
* @param s input string
* @param sub characters to replace
* @param with characters to replace with
*/
public static String replaceChars(final String s, final char[] sub, final char[] with) {
char[] str = s.toCharArray();
for (int i = 0; i < str.length; i++) {
char c = str[i];
for (int j = 0; j < sub.length; j++) {
if (c == sub[j]) {
str[i] = with[j];
break;
}
}
}
return new String(str);
}
/**
* Replaces the very first occurrence of a substring with supplied string.
*
* @param s source string
* @param sub substring to replace
* @param with substring to replace with
*/
public static String replaceFirst(final String s, final String sub, final String with) {
int i = s.indexOf(sub);
if (i == -1) {
return s;
}
return s.substring(0, i) + with + s.substring(i + sub.length());
}
/**
* Replaces the very first occurrence of a character in a string.
*
* @param s string
* @param sub char to replace
* @param with char to replace with
*/
public static String replaceFirst(final String s, final char sub, final char with) {
int index = s.indexOf(sub);
if (index == -1) {
return s;
}
char[] str = s.toCharArray();
str[index] = with;
return new String(str);
}
/**
* Replaces the very last occurrence of a substring with supplied string.
*
* @param s source string
* @param sub substring to replace
* @param with substring to replace with
*/
public static String replaceLast(final String s, final String sub, final String with) {
int i = s.lastIndexOf(sub);
if (i == -1) {
return s;
}
return s.substring(0, i) + with + s.substring(i + sub.length());
}
/**
* Replaces the very last occurrence of a character in a string.
*
* @param s string
* @param sub char to replace
* @param with char to replace with
*/
public static String replaceLast(final String s, final char sub, final char with) {
int index = s.lastIndexOf(sub);
if (index == -1) {
return s;
}
char[] str = s.toCharArray();
str[index] = with;
return new String(str);
}
// ---------------------------------------------------------------- remove
/**
* Removes all substring occurrences from the string.
*
* @param s source string
* @param sub substring to remove
*/
public static String remove(final String s, final String sub) {
int c = 0;
int sublen = sub.length();
if (sublen == 0) {
return s;
}
int i = s.indexOf(sub, c);
if (i == -1) {
return s;
}
StringBuilder sb = new StringBuilder(s.length());
do {
sb.append(s, c, i);
c = i + sublen;
} while ((i = s.indexOf(sub, c)) != -1);
if (c < s.length()) {
sb.append(s, c, s.length());
}
return sb.toString();
}
/**
* Removes all characters contained in provided string.
*
* @param src source string
* @param chars string containing characters to remove
*/
public static String removeChars(final String src, final String chars) {
int i = src.length();
StringBuilder sb = new StringBuilder(i);
for (int j = 0; j < i; j++) {
char c = src.charAt(j);
if (chars.indexOf(c) == -1) {
sb.append(c);
}
}
return sb.toString();
}
/**
* Removes set of characters from string.
*
* @param src string
* @param chars characters to remove
*/
public static String removeChars(final String src, final char... chars) {
int i = src.length();
StringBuilder sb = new StringBuilder(i);
mainloop:
for (int j = 0; j < i; j++) {
char c = src.charAt(j);
for (char aChar : chars) {
if (c == aChar) {
continue mainloop;
}
}
sb.append(c);
}
return sb.toString();
}
/**
* Removes a single character from string.
*
* @param string source string
* @param ch character to remove
*/
public static String remove(final String string, final char ch) {
int stringLen = string.length();
char[] result = new char[stringLen];
int offset = 0;
for (int i = 0; i < stringLen; i++) {
char c = string.charAt(i);
if (c == ch) {
continue;
}
result[offset] = c;
offset++;
}
if (offset == stringLen) {
return string; // no changes
}
return new String(result, 0, offset);
}
// ---------------------------------------------------------------- miscellaneous
/**
* Compares 2 strings. If one of the strings is <code>null</code>, <code>false</code> is returned. if
* both string are <code>null</code>, <code>true</code> is returned.
*
* @param s1 first string to compare
* @param s2 second string
*
* @return <code>true</code> if strings are equal, otherwise <code>false</code>
*/
public static boolean equals(final String s1, final String s2) {
return Util.equals(s1, s2);
}
/**
* Determines if a string is empty (<code>null</code> or zero-length).
*/
public static boolean isEmpty(final CharSequence string) {
return ((string == null) || (string.length() == 0));
}
/**
* Determines if string array contains empty strings.
* @see #isEmpty(CharSequence)
*/
public static boolean isAllEmpty(final String... strings) {
for (String string : strings) {
if (!isEmpty(string)) {
return false;
}
}
return true;
}
/**
* Determines if a string is blank (<code>null</code> or {@link #containsOnlyWhitespaces(CharSequence)}).
*/
public static boolean isBlank(final CharSequence string) {
return ((string == null) || containsOnlyWhitespaces(string));
}
/**
* Determines if string is not blank.
*/
public static boolean isNotBlank(final CharSequence string) {
return ((string != null) && !containsOnlyWhitespaces(string));
}
/**
* Determines if string array contains just blank strings.
*/
public static boolean isAllBlank(final String... strings) {
for (String string : strings) {
if (!isBlank(string)) {
return false;
}
}
return true;
}
/**
* Returns <code>true</code> if string contains only white spaces.
*/
public static boolean containsOnlyWhitespaces(final CharSequence string) {
int size = string.length();
for (int i = 0; i < size; i++) {
char c = string.charAt(i);
if (!CharUtil.isWhitespace(c)) {
return false;
}
}
return true;
}
/**
* Returns <code>true</code> if string contains only digits.
*/
public static boolean containsOnlyDigits(final CharSequence string) {
int size = string.length();
for (int i = 0; i < size; i++) {
char c = string.charAt(i);
if (!CharUtil.isDigit(c)) {
return false;
}
}
return true;
}
/**
* Returns <code>true</code> if string {@link #containsOnlyDigits(CharSequence) contains only digits}
* or signs plus or minus.
*/
public static boolean containsOnlyDigitsAndSigns(final CharSequence string) {
int size = string.length();
for (int i = 0; i < size; i++) {
char c = string.charAt(i);
if ((!CharUtil.isDigit(c)) && (c != '-') && (c != '+')) {
return false;
}
}
return true;
}
/**
* Determines if a string is not empty.
*/
public static boolean isNotEmpty(final CharSequence string) {
return string != null && string.length() > 0;
}
/**
* Converts safely an object to a string.
*/
public static String toString(final Object value) {
if (value == null) {
return null;
}
return value.toString();
}
/**
* Converts safely an object to a string. If object is <code>null</code> an empty
* string is returned.
*/
public static String toSafeString(final Object value) {
if (value == null) {
return EMPTY;
}
return value.toString();
}
/**
* Converts an array object to array of strings, where every element
* of input array is converted to a string. If input is not an array,
* the result will still be an array with one element.
*/
public static String[] toStringArray(final Object value) {
if (value == null) {
return new String[0];
}
Class<?> type = value.getClass();
if (!type.isArray()) {
return new String[] {value.toString()};
}
Class componentType = type.getComponentType();
if (componentType.isPrimitive()) {
if (componentType == int.class) {
return ArraysUtil.toStringArray((int[]) value);
}
else if (componentType == long.class) {
return ArraysUtil.toStringArray((long[]) value);
}
else if (componentType == double.class) {
return ArraysUtil.toStringArray((double[]) value);
}
else if (componentType == float.class) {
return ArraysUtil.toStringArray((float[]) value);
}
else if (componentType == boolean.class) {
return ArraysUtil.toStringArray((boolean[]) value);
}
else if (componentType == short.class) {
return ArraysUtil.toStringArray((short[]) value);
}
else if (componentType == byte.class) {
return ArraysUtil.toStringArray((byte[]) value);
}
else {
throw new IllegalArgumentException();
}
}
else {
return ArraysUtil.toStringArray((Object[]) value);
}
}
// ---------------------------------------------------------------- capitalize
/**
* Capitalizes a string, changing the first letter to
* upper case. No other letters are changed.
*
* @param str string to capitalize, may be null
* @see #uncapitalize(String)
*/
public static String capitalize(final String str) {
return changeFirstCharacterCase(true, str);
}
/**
* Uncapitalizes a <code>String</code>, changing the first letter to
* lower case. No other letters are changed.
*
* @param str the String to uncapitalize, may be null
* @return the uncapitalized String, <code>null</code> if null
* @see #capitalize(String)
*/
public static String uncapitalize(final String str) {
return changeFirstCharacterCase(false, str);
}
/**
* Internal method for changing the first character case.
*/
private static String changeFirstCharacterCase(final boolean capitalize, final String string) {
int strLen = string.length();
if (strLen == 0) {
return string;
}
char ch = string.charAt(0);
char modifiedCh;
if (capitalize) {
modifiedCh = Character.toUpperCase(ch);
} else {
modifiedCh = Character.toLowerCase(ch);
}
if (modifiedCh == ch) {
// no change, return unchanged string
return string;
}
char[] chars = string.toCharArray();
chars[0] = modifiedCh;
return new String(chars);
}
/**
* Utility method to take a string and convert it to normal Java variable
* name capitalization. This normally means converting the first
* character from upper case to lower case, but in the (unusual) special
* case when there is more than one character and both the first and
* second characters are upper case, we leave it alone.
* <p>
* Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
* as "URL".
*
* @param name The string to be decapitalized.
* @return The decapitalized version of the string.
*/
public static String decapitalize(final String name) {
if (name.length() == 0) {
return name;
}
if (name.length() > 1 &&
Character.isUpperCase(name.charAt(1)) &&
Character.isUpperCase(name.charAt(0))) {
return name;
}
char[] chars = name.toCharArray();
char c = chars[0];
char modifiedChar = Character.toLowerCase(c);
if (modifiedChar == c) {
return name;
}
chars[0] = modifiedChar;
return new String(chars);
}
/**
* Makes a title-cased string from given input.
*/
public static String title(final String string) {
char[] chars = string.toCharArray();
boolean wasWhitespace = true;
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (CharUtil.isWhitespace(c)) {
wasWhitespace = true;
} else {
if (wasWhitespace) {
chars[i] = Character.toUpperCase(c);
} else {
chars[i] = Character.toLowerCase(c);
}
wasWhitespace = false;
}
}
return new String(chars);
}
// ---------------------------------------------------------------- truncate
/**
* Sets the maximum length of the string. Longer strings will be simply truncated.
*/
public static String truncate(String string, final int length) {
if (string.length() > length) {
string = string.substring(0, length);
}
return string;
}
// ---------------------------------------------------------------- substring
/**
* Returns a new string that is a substring of this string. The substring
* begins at the specified <code>fromIndex</code> and extends to the character
* at index <code>toIndex - 1</code>. However, index values can be negative,
* and then the real index will be calculated from the strings end. This
* allows to specify, e.g. <code>substring(1,-1)</code> to cut one character
* from both ends of the string. If <code>fromIndex</code> is negative
* and <code>toIndex</code> is 0, it will return last characters of the string.
* Also, this method will never throw an exception if index is out of range.
*/
public static String substring(final String string, int fromIndex, int toIndex) {
int len = string.length();
if (fromIndex < 0) {
fromIndex = len + fromIndex;
if (toIndex == 0) {
toIndex = len;
}
}
if (toIndex < 0) {
toIndex = len + toIndex;
}
// safe net
if (fromIndex < 0) {
fromIndex = 0;
}
if (toIndex > len) {
toIndex = len;
}
if (fromIndex >= toIndex) {
return StringPool.EMPTY;
}
return string.substring(fromIndex, toIndex);
}
/**
* Returns <code>true</code> if substring exist at given offset in a string.
*/
public static boolean isSubstringAt(final String string, final String substring, final int offset) {
int len = substring.length();
int max = offset + len;
if (max > string.length()) {
return false;
}
int ndx = 0;
for (int i = offset; i < max; i++, ndx++) {
if (string.charAt(i) != substring.charAt(ndx)) {
return false;
}
}
return true;
}
// ---------------------------------------------------------------- split
/**
* Splits a string in several parts (tokens) that are separated by delimiter.
* Delimiter is <b>always</b> surrounded by two strings! If there is no
* content between two delimiters, empty string will be returned for that
* token. Therefore, the length of the returned array will always be:
* #delimiters + 1.
* <p>
* Method is much, much faster then regexp <code>String.split()</code>,
* and a bit faster then <code>StringTokenizer</code>.
*
* @param src string to split
* @param delimiter split delimiter
*
* @return array of split strings
*/
public static String[] split(final String src, final String delimiter) {
int maxparts = (src.length() / delimiter.length()) + 2; // one more for the last
int[] positions = new int[maxparts];
int dellen = delimiter.length();
int i, j = 0;
int count = 0;
positions[0] = - dellen;
while ((i = src.indexOf(delimiter, j)) != -1) {
count++;
positions[count] = i;
j = i + dellen;
}
count++;
positions[count] = src.length();
String[] result = new String[count];
for (i = 0; i < count; i++) {
result[i] = src.substring(positions[i] + dellen, positions[i + 1]);
}
return result;
}
/**
* Splits a string in several parts (tokens) that are separated by delimiter
* characters. Delimiter may contains any number of character and it is
* always surrounded by two strings.
*
* @param src source to examine
* @param d string with delimiter characters
*
* @return array of tokens
*/
public static String[] splitc(final String src, final String d) {
if ((d.length() == 0) || (src.length() == 0)) {
return new String[] {src};
}
return splitc(src, d.toCharArray());
}
/**
* Splits a string in several parts (tokens) that are separated by delimiter
* characters. Delimiter may contains any number of character and it is
* always surrounded by two strings.
*
* @param src source to examine
* @param delimiters char array with delimiter characters
*
* @return array of tokens
*/
public static String[] splitc(final String src, final char[] delimiters) {
if ((delimiters.length == 0) || (src.length() == 0) ) {
return new String[] {src};
}
char[] srcc = src.toCharArray();
int maxparts = srcc.length + 1;
int[] start = new int[maxparts];
int[] end = new int[maxparts];
int count = 0;
start[0] = 0;
int s = 0, e;
if (CharUtil.equalsOne(srcc[0], delimiters)) { // string starts with delimiter
end[0] = 0;
count++;
s = CharUtil.findFirstDiff(srcc, 1, delimiters);
if (s == -1) { // nothing after delimiters
return new String[] {EMPTY, EMPTY};
}
start[1] = s; // new start
}
while (true) {
// find new end
e = CharUtil.findFirstEqual(srcc, s, delimiters);
if (e == -1) {
end[count] = srcc.length;
break;
}
end[count] = e;
// find new start
count++;
s = CharUtil.findFirstDiff(srcc, e, delimiters);
if (s == -1) {
start[count] = end[count] = srcc.length;
break;
}
start[count] = s;
}
count++;
String[] result = new String[count];
for (int i = 0; i < count; i++) {
result[i] = src.substring(start[i], end[i]);
}
return result;
}
/**
* Splits a string in several parts (tokens) that are separated by single delimiter
* characters. Delimiter is always surrounded by two strings.
*
* @param src source to examine
* @param delimiter delimiter character
*
* @return array of tokens
*/
public static String[] splitc(final String src, final char delimiter) {
if (src.length() == 0) {
return new String[] {EMPTY};
}
char[] srcc = src.toCharArray();
int maxparts = srcc.length + 1;
int[] start = new int[maxparts];
int[] end = new int[maxparts];
int count = 0;
start[0] = 0;
int s = 0, e;
if (srcc[0] == delimiter) { // string starts with delimiter
end[0] = 0;
count++;
s = CharUtil.findFirstDiff(srcc, 1, delimiter);
if (s == -1) { // nothing after delimiters
return new String[] {EMPTY, EMPTY};
}
start[1] = s; // new start
}
while (true) {
// find new end
e = CharUtil.findFirstEqual(srcc, s, delimiter);
if (e == -1) {
end[count] = srcc.length;
break;
}
end[count] = e;
// find new start
count++;
s = CharUtil.findFirstDiff(srcc, e, delimiter);
if (s == -1) {
start[count] = end[count] = srcc.length;
break;
}
start[count] = s;
}
count++;
String[] result = new String[count];
for (int i = 0; i < count; i++) {
result[i] = src.substring(start[i], end[i]);
}
return result;
}
/**
* Compress multiple occurrences of given char into one appearance.
*/
public static String compressChars(final String s, final char c) {
int len = s.length();
StringBuilder sb = new StringBuilder(len);
boolean wasChar = false;
for (int i = 0; i < len; i++) {
char c1 = s.charAt(i);
if (c1 == c) {
if (wasChar) {
continue;
}
wasChar = true;
} else {
wasChar = false;
}
sb.append(c1);
}
if (sb.length() == len) {
return s;
}
return sb.toString();
}
// ---------------------------------------------------------------- indexof and ignore cases
/**
* Finds first occurrence of a substring in the given source but within limited range [start, end).
* It is fastest possible code, but still original <code>String.indexOf(String, int)</code>
* is much faster (since it uses char[] value directly) and should be used when no range is needed.
*
* @param src source string for examination
* @param sub substring to find
* @param startIndex starting index
* @param endIndex ending index
* @return index of founded substring or -1 if substring not found
*/
public static int indexOf(final String src, final String sub, int startIndex, int endIndex) {
if (startIndex < 0) {
startIndex = 0;
}
int srclen = src.length();
if (endIndex > srclen) {
endIndex = srclen;
}
int sublen = sub.length();
if (sublen == 0) {
return startIndex > srclen ? srclen : startIndex;
}
int total = endIndex - sublen + 1;
char c = sub.charAt(0);
mainloop:
for (int i = startIndex; i < total; i++) {
if (src.charAt(i) != c) {
continue;
}
int j = 1;
int k = i + 1;
while (j < sublen) {
if (sub.charAt(j) != src.charAt(k)) {
continue mainloop;
}
j++; k++;
}
return i;
}
return -1;
}
/**
* Finds the first occurrence of a character in the given source but within limited range (start, end].
*/
public static int indexOf(final String src, final char c, int startIndex, int endIndex) {
if (startIndex < 0) {
startIndex = 0;
}
int srclen = src.length();
if (endIndex > srclen) {
endIndex = srclen;
}
for (int i = startIndex; i < endIndex; i++) {
if (src.charAt(i) == c) {
return i;
}
}
return -1;
}
/**
* Finds the first occurrence of a character in the given source but within limited range (start, end].
*/
public static int indexOfIgnoreCase(final String src, char c, int startIndex, int endIndex) {
if (startIndex < 0) {
startIndex = 0;
}
int srclen = src.length();
if (endIndex > srclen) {
endIndex = srclen;
}
c = Character.toLowerCase(c);
for (int i = startIndex; i < endIndex; i++) {
if (Character.toLowerCase(src.charAt(i)) == c) {
return i;
}
}
return -1;
}
/**
* Finds first index of a substring in the given source string with ignored case.
*
* @param src source string for examination
* @param subS substring to find
*
* @return index of founded substring or -1 if substring is not found
* @see #indexOfIgnoreCase(String, String, int)
*/
public static int indexOfIgnoreCase(final String src, final String subS) {
return indexOfIgnoreCase(src, subS, 0, src.length());
}
/**
* Finds first index of a substring in the given source string with ignored
* case. This seems to be the fastest way doing this, with common string
* length and content (of course, with no use of Boyer-Mayer type of
* algorithms). Other implementations are slower: getting char array first,
* lower casing the source string, using String.regionMatch etc.
*
* @param src source string for examination
* @param subS substring to find
* @param startIndex starting index from where search begins
*
* @return index of founded substring or -1 if substring is not found
*/
public static int indexOfIgnoreCase(final String src, final String subS, final int startIndex) {
return indexOfIgnoreCase(src, subS, startIndex, src.length());
}
/**
* Finds first index of a substring in the given source string and range with
* ignored case.
*
* @param src source string for examination
* @param sub substring to find
* @param startIndex starting index from where search begins
* @param endIndex endint index
* @return index of founded substring or -1 if substring is not found
* @see #indexOfIgnoreCase(String, String, int)
*/
public static int indexOfIgnoreCase(final String src, String sub, int startIndex, int endIndex) {
if (startIndex < 0) {
startIndex = 0;
}
int srclen = src.length();
if (endIndex > srclen) {
endIndex = srclen;
}
int sublen = sub.length();
if (sublen == 0) {
return startIndex > srclen ? srclen : startIndex;
}
sub = sub.toLowerCase();
int total = endIndex - sublen + 1;
char c = sub.charAt(0);
mainloop:
for (int i = startIndex; i < total; i++) {