1
22
23 package com.liferay.portal.kernel.util;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.io.StringReader;
33
34 import java.net.URL;
35
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.Enumeration;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.StringTokenizer;
42
43
51 public class StringUtil {
52
53 public static String add(String s, String add) {
54 return add(s, add, StringPool.COMMA);
55 }
56
57 public static String add(String s, String add, String delimiter) {
58 return add(s, add, delimiter, false);
59 }
60
61 public static String add(
62 String s, String add, String delimiter, boolean allowDuplicates) {
63
64 if ((add == null) || (delimiter == null)) {
65 return null;
66 }
67
68 if (s == null) {
69 s = StringPool.BLANK;
70 }
71
72 if (allowDuplicates || !contains(s, add, delimiter)) {
73 StringBuilder sb = new StringBuilder();
74
75 sb.append(s);
76
77 if (Validator.isNull(s) || s.endsWith(delimiter)) {
78 sb.append(add);
79 sb.append(delimiter);
80 }
81 else {
82 sb.append(delimiter);
83 sb.append(add);
84 sb.append(delimiter);
85 }
86
87 s = sb.toString();
88 }
89
90 return s;
91 }
92
93 public static String bytesToHexString(byte[] bytes) {
94 StringBuilder sb = new StringBuilder(bytes.length * 2);
95
96 for (int i = 0; i < bytes.length; i++) {
97 String hex = Integer.toHexString(
98 0x0100 + (bytes[i] & 0x00FF)).substring(1);
99
100 if (hex.length() < 2) {
101 sb.append("0");
102 }
103
104 sb.append(hex);
105 }
106
107 return sb.toString();
108 }
109
110 public static boolean contains(String s, String text) {
111 return contains(s, text, StringPool.COMMA);
112 }
113
114 public static boolean contains(String s, String text, String delimiter) {
115 if ((s == null) || (text == null) || (delimiter == null)) {
116 return false;
117 }
118
119 StringBuilder sb = null;
120
121 if (!s.endsWith(delimiter)) {
122 sb = new StringBuilder();
123
124 sb.append(s);
125 sb.append(delimiter);
126
127 s = sb.toString();
128 }
129
130 sb = new StringBuilder();
131
132 sb.append(delimiter);
133 sb.append(text);
134 sb.append(delimiter);
135
136 String dtd = sb.toString();
137
138 int pos = s.indexOf(dtd);
139
140 if (pos == -1) {
141 sb = new StringBuilder();
142
143 sb.append(text);
144 sb.append(delimiter);
145
146 String td = sb.toString();
147
148 if (s.startsWith(td)) {
149 return true;
150 }
151
152 return false;
153 }
154
155 return true;
156 }
157
158 public static int count(String s, String text) {
159 if ((s == null) || (text == null)) {
160 return 0;
161 }
162
163 int count = 0;
164
165 int pos = s.indexOf(text);
166
167 while (pos != -1) {
168 pos = s.indexOf(text, pos + text.length());
169
170 count++;
171 }
172
173 return count;
174 }
175
176 public static boolean endsWith(String s, char end) {
177 return endsWith(s, (new Character(end)).toString());
178 }
179
180 public static boolean endsWith(String s, String end) {
181 if ((s == null) || (end == null)) {
182 return false;
183 }
184
185 if (end.length() > s.length()) {
186 return false;
187 }
188
189 String temp = s.substring(s.length() - end.length(), s.length());
190
191 if (temp.equalsIgnoreCase(end)) {
192 return true;
193 }
194 else {
195 return false;
196 }
197 }
198
199 public static String extractChars(String s) {
200 if (s == null) {
201 return StringPool.BLANK;
202 }
203
204 StringBuilder sb = new StringBuilder();
205
206 char[] c = s.toCharArray();
207
208 for (int i = 0; i < c.length; i++) {
209 if (Validator.isChar(c[i])) {
210 sb.append(c[i]);
211 }
212 }
213
214 return sb.toString();
215 }
216
217 public static String extractDigits(String s) {
218 if (s == null) {
219 return StringPool.BLANK;
220 }
221
222 StringBuilder sb = new StringBuilder();
223
224 char[] c = s.toCharArray();
225
226 for (int i = 0; i < c.length; i++) {
227 if (Validator.isDigit(c[i])) {
228 sb.append(c[i]);
229 }
230 }
231
232 return sb.toString();
233 }
234
235 public static String extractFirst(String s, String delimiter) {
236 if (s == null) {
237 return null;
238 }
239 else {
240 String[] array = split(s, delimiter);
241
242 if (array.length > 0) {
243 return array[0];
244 }
245 else {
246 return null;
247 }
248 }
249 }
250
251 public static String extractLast(String s, String delimiter) {
252 if (s == null) {
253 return null;
254 }
255 else {
256 String[] array = split(s, delimiter);
257
258 if (array.length > 0) {
259 return array[array.length - 1];
260 }
261 else {
262 return null;
263 }
264 }
265 }
266
267 public static String highlight(String s, String keywords) {
268 return highlight(s, keywords, "<span class=\"highlight\">", "</span>");
269 }
270
271 public static String highlight(
272 String s, String keywords, String highlight1, String highlight2) {
273
274 if (s == null) {
275 return null;
276 }
277
278
283
286 StringBuilder sb = new StringBuilder(StringPool.SPACE);
287
288 StringTokenizer st = new StringTokenizer(s);
289
290 while (st.hasMoreTokens()) {
291 String token = st.nextToken();
292
293 if (token.equalsIgnoreCase(keywords)) {
294 sb.append(highlight1);
295 sb.append(token);
296 sb.append(highlight2);
297 }
298 else {
299 sb.append(token);
300 }
301
302 if (st.hasMoreTokens()) {
303 sb.append(StringPool.SPACE);
304 }
305 }
306
307 return sb.toString();
308 }
309
310 public static String lowerCase(String s) {
311 if (s == null) {
312 return null;
313 }
314 else {
315 return s.toLowerCase();
316 }
317 }
318
319 public static String merge(boolean[] array) {
320 return merge(array, StringPool.COMMA);
321 }
322
323 public static String merge(boolean[] array, String delimiter) {
324 if (array == null) {
325 return null;
326 }
327
328 StringBuilder sb = new StringBuilder();
329
330 for (int i = 0; i < array.length; i++) {
331 sb.append(String.valueOf(array[i]).trim());
332
333 if ((i + 1) != array.length) {
334 sb.append(delimiter);
335 }
336 }
337
338 return sb.toString();
339 }
340
341 public static String merge(double[] array) {
342 return merge(array, StringPool.COMMA);
343 }
344
345 public static String merge(double[] array, String delimiter) {
346 if (array == null) {
347 return null;
348 }
349
350 StringBuilder sb = new StringBuilder();
351
352 for (int i = 0; i < array.length; i++) {
353 sb.append(String.valueOf(array[i]).trim());
354
355 if ((i + 1) != array.length) {
356 sb.append(delimiter);
357 }
358 }
359
360 return sb.toString();
361 }
362
363 public static String merge(float[] array) {
364 return merge(array, StringPool.COMMA);
365 }
366
367 public static String merge(float[] array, String delimiter) {
368 if (array == null) {
369 return null;
370 }
371
372 StringBuilder sb = new StringBuilder();
373
374 for (int i = 0; i < array.length; i++) {
375 sb.append(String.valueOf(array[i]).trim());
376
377 if ((i + 1) != array.length) {
378 sb.append(delimiter);
379 }
380 }
381
382 return sb.toString();
383 }
384
385 public static String merge(int[] array) {
386 return merge(array, StringPool.COMMA);
387 }
388
389 public static String merge(int[] array, String delimiter) {
390 if (array == null) {
391 return null;
392 }
393
394 StringBuilder sb = new StringBuilder();
395
396 for (int i = 0; i < array.length; i++) {
397 sb.append(String.valueOf(array[i]).trim());
398
399 if ((i + 1) != array.length) {
400 sb.append(delimiter);
401 }
402 }
403
404 return sb.toString();
405 }
406
407 public static String merge(long[] array) {
408 return merge(array, StringPool.COMMA);
409 }
410
411 public static String merge(long[] array, String delimiter) {
412 if (array == null) {
413 return null;
414 }
415
416 StringBuilder sb = new StringBuilder();
417
418 for (int i = 0; i < array.length; i++) {
419 sb.append(String.valueOf(array[i]).trim());
420
421 if ((i + 1) != array.length) {
422 sb.append(delimiter);
423 }
424 }
425
426 return sb.toString();
427 }
428
429 public static String merge(short[] array) {
430 return merge(array, StringPool.COMMA);
431 }
432
433 public static String merge(short[] array, String delimiter) {
434 if (array == null) {
435 return null;
436 }
437
438 StringBuilder sb = new StringBuilder();
439
440 for (int i = 0; i < array.length; i++) {
441 sb.append(String.valueOf(array[i]).trim());
442
443 if ((i + 1) != array.length) {
444 sb.append(delimiter);
445 }
446 }
447
448 return sb.toString();
449 }
450
451 public static String merge(Collection<?> col) {
452 return merge(col, StringPool.COMMA);
453 }
454
455 public static String merge(Collection<?> col, String delimiter) {
456 if (col == null) {
457 return null;
458 }
459
460 return merge(col.toArray(new Object[col.size()]), delimiter);
461 }
462
463 public static String merge(Object[] array) {
464 return merge(array, StringPool.COMMA);
465 }
466
467 public static String merge(Object[] array, String delimiter) {
468 if (array == null) {
469 return null;
470 }
471
472 StringBuilder sb = new StringBuilder();
473
474 for (int i = 0; i < array.length; i++) {
475 sb.append(String.valueOf(array[i]).trim());
476
477 if ((i + 1) != array.length) {
478 sb.append(delimiter);
479 }
480 }
481
482 return sb.toString();
483 }
484
485 public static String randomize(String s) {
486 return Randomizer.getInstance().randomize(s);
487 }
488
489 public static String read(ClassLoader classLoader, String name)
490 throws IOException {
491
492 return read(classLoader, name, false);
493 }
494
495 public static String read(ClassLoader classLoader, String name, boolean all)
496 throws IOException {
497
498 if (all) {
499 StringBuilder sb = new StringBuilder();
500
501 Enumeration<URL> enu = classLoader.getResources(name);
502
503 while (enu.hasMoreElements()) {
504 URL url = enu.nextElement();
505
506 InputStream is = url.openStream();
507
508 String s = read(is);
509
510 if (s != null) {
511 sb.append(s);
512 sb.append(StringPool.NEW_LINE);
513 }
514
515 is.close();
516 }
517
518 return sb.toString().trim();
519 }
520 else {
521 InputStream is = classLoader.getResourceAsStream(name);
522
523 String s = read(is);
524
525 is.close();
526
527 return s;
528 }
529 }
530
531 public static String read(InputStream is) throws IOException {
532 StringBuilder sb = new StringBuilder();
533
534 BufferedReader br = new BufferedReader(new InputStreamReader(is));
535
536 String line = null;
537
538 while ((line = br.readLine()) != null) {
539 sb.append(line).append('\n');
540 }
541
542 br.close();
543
544 return sb.toString().trim();
545 }
546
547 public static String remove(String s, String remove) {
548 return remove(s, remove, StringPool.COMMA);
549 }
550
551 public static String remove(String s, String remove, String delimiter) {
552 if ((s == null) || (remove == null) || (delimiter == null)) {
553 return null;
554 }
555
556 if (Validator.isNotNull(s) && !s.endsWith(delimiter)) {
557 s += delimiter;
558 }
559
560 StringBuilder sb = new StringBuilder();
561
562 sb.append(delimiter);
563 sb.append(remove);
564 sb.append(delimiter);
565
566 String drd = sb.toString();
567
568 sb = new StringBuilder();
569
570 sb.append(remove);
571 sb.append(delimiter);
572
573 String rd = sb.toString();
574
575 while (contains(s, remove, delimiter)) {
576 int pos = s.indexOf(drd);
577
578 if (pos == -1) {
579 if (s.startsWith(rd)) {
580 int x = remove.length() + delimiter.length();
581 int y = s.length();
582
583 s = s.substring(x, y);
584 }
585 }
586 else {
587 int x = pos + remove.length() + delimiter.length();
588 int y = s.length();
589
590 sb = new StringBuilder();
591
592 sb.append(s.substring(0, pos));
593 sb.append(s.substring(x, y));
594
595 s = sb.toString();
596 }
597 }
598
599 return s;
600 }
601
602 public static String replace(String s, char oldSub, char newSub) {
603 if (s == null) {
604 return null;
605 }
606
607 return s.replace(oldSub, newSub);
608 }
609
610 public static String replace(String s, char oldSub, String newSub) {
611 if ((s == null) || (newSub == null)) {
612 return null;
613 }
614
615
618 StringBuilder sb = new StringBuilder(s.length() + 5 * newSub.length());
619
620 char[] charArray = s.toCharArray();
621
622 for (char c : charArray) {
623 if (c == oldSub) {
624 sb.append(newSub);
625 }
626 else {
627 sb.append(c);
628 }
629 }
630
631 return sb.toString();
632 }
633
634 public static String replace(String s, String oldSub, String newSub) {
635 if ((s == null) || (oldSub == null) || (newSub == null)) {
636 return null;
637 }
638
639 int y = s.indexOf(oldSub);
640
641 if (y >= 0) {
642
643
646 StringBuilder sb = new StringBuilder(
647 s.length() + 5 * newSub.length());
648
649 int length = oldSub.length();
650 int x = 0;
651
652 while (x <= y) {
653 sb.append(s.substring(x, y));
654 sb.append(newSub);
655
656 x = y + length;
657 y = s.indexOf(oldSub, x);
658 }
659
660 sb.append(s.substring(x));
661
662 return sb.toString();
663 }
664 else {
665 return s;
666 }
667 }
668
669 public static String replace(String s, String[] oldSubs, String[] newSubs) {
670 if ((s == null) || (oldSubs == null) || (newSubs == null)) {
671 return null;
672 }
673
674 if (oldSubs.length != newSubs.length) {
675 return s;
676 }
677
678 for (int i = 0; i < oldSubs.length; i++) {
679 s = replace(s, oldSubs[i], newSubs[i]);
680 }
681
682 return s;
683 }
684
685 public static String replace(
686 String s, String[] oldSubs, String[] newSubs, boolean exactMatch) {
687
688 if ((s == null) || (oldSubs == null) || (newSubs == null)) {
689 return null;
690 }
691
692 if (oldSubs.length != newSubs.length) {
693 return s;
694 }
695
696 if (!exactMatch) {
697 replace(s, oldSubs, newSubs);
698 }
699 else {
700 for (int i = 0; i < oldSubs.length; i++) {
701 s = s.replaceAll("\\b" + oldSubs[i] + "\\b" , newSubs[i]);
702 }
703 }
704
705 return s;
706 }
707
708 public static String replaceFirst(String s, char oldSub, char newSub) {
709 if (s == null) {
710 return null;
711 }
712
713 return s.replaceFirst(String.valueOf(oldSub), String.valueOf(newSub));
714 }
715
716 public static String replaceFirst(String s, char oldSub, String newSub) {
717 if ((s == null) || (newSub == null)) {
718 return null;
719 }
720
721 return s.replaceFirst(String.valueOf(oldSub), newSub);
722 }
723
724 public static String replaceFirst(String s, String oldSub, String newSub) {
725 if ((s == null) || (oldSub == null) || (newSub == null)) {
726 return null;
727 }
728
729 return s.replaceFirst(oldSub, newSub);
730 }
731
732 public static String replaceFirst(
733 String s, String[] oldSubs, String[] newSubs) {
734
735 if ((s == null) || (oldSubs == null) || (newSubs == null)) {
736 return null;
737 }
738
739 if (oldSubs.length != newSubs.length) {
740 return s;
741 }
742
743 for (int i = 0; i < oldSubs.length; i++) {
744 s = replaceFirst(s, oldSubs[i], newSubs[i]);
745 }
746
747 return s;
748 }
749
750
766 public static String replaceValues(
767 String s, String begin, String end, Map<String, String> values) {
768
769 if ((s == null) || (begin == null) || (end == null) ||
770 (values == null) || (values.size() == 0)) {
771
772 return s;
773 }
774
775 StringBuilder sb = new StringBuilder(s.length());
776
777 int pos = 0;
778
779 while (true) {
780 int x = s.indexOf(begin, pos);
781 int y = s.indexOf(end, x + begin.length());
782
783 if ((x == -1) || (y == -1)) {
784 sb.append(s.substring(pos, s.length()));
785
786 break;
787 }
788 else {
789 sb.append(s.substring(pos, x + begin.length()));
790
791 String oldValue = s.substring(x + begin.length(), y);
792
793 String newValue = values.get(oldValue);
794
795 if (newValue == null) {
796 newValue = oldValue;
797 }
798
799 sb.append(newValue);
800
801 pos = y;
802 }
803 }
804
805 return sb.toString();
806 }
807
808 public static String reverse(String s) {
809 if (s == null) {
810 return null;
811 }
812
813 char[] c = s.toCharArray();
814 char[] reverse = new char[c.length];
815
816 for (int i = 0; i < c.length; i++) {
817 reverse[i] = c[c.length - i - 1];
818 }
819
820 return new String(reverse);
821 }
822
823 public static String safePath(String path) {
824 return replace(path, StringPool.DOUBLE_SLASH, StringPool.SLASH);
825 }
826
827 public static String shorten(String s) {
828 return shorten(s, 20);
829 }
830
831 public static String shorten(String s, int length) {
832 return shorten(s, length, "...");
833 }
834
835 public static String shorten(String s, String suffix) {
836 return shorten(s, 20, suffix);
837 }
838
839 public static String shorten(String s, int length, String suffix) {
840 if ((s == null) || (suffix == null)) {
841 return null;
842 }
843
844 if (s.length() > length) {
845 for (int j = length; j >= 0; j--) {
846 if (Character.isWhitespace(s.charAt(j))) {
847 length = j;
848
849 break;
850 }
851 }
852
853 StringBuilder sb = new StringBuilder();
854
855 sb.append(s.substring(0, length));
856 sb.append(suffix);
857
858 s = sb.toString();
859 }
860
861 return s;
862 }
863
864 public static String[] split(String s) {
865 return split(s, StringPool.COMMA);
866 }
867
868 public static String[] split(String s, String delimiter) {
869 if ((Validator.isNull(s)) || (delimiter == null) ||
870 (delimiter.equals(StringPool.BLANK))) {
871
872 return new String[0];
873 }
874
875 s = s.trim();
876
877 if (!s.endsWith(delimiter)) {
878 StringBuilder sb = new StringBuilder();
879
880 sb.append(s);
881 sb.append(delimiter);
882
883 s = sb.toString();
884 }
885
886 if (s.equals(delimiter)) {
887 return new String[0];
888 }
889
890 List<String> nodeValues = new ArrayList<String>();
891
892 if (delimiter.equals(StringPool.NEW_LINE) ||
893 delimiter.equals(StringPool.RETURN)) {
894
895 try {
896 BufferedReader br = new BufferedReader(new StringReader(s));
897
898 String line = null;
899
900 while ((line = br.readLine()) != null) {
901 nodeValues.add(line);
902 }
903
904 br.close();
905 }
906 catch (IOException ioe) {
907 _log.error(ioe.getMessage());
908 }
909 }
910 else {
911 int offset = 0;
912 int pos = s.indexOf(delimiter, offset);
913
914 while (pos != -1) {
915 nodeValues.add(new String(s.substring(offset, pos)));
916
917 offset = pos + delimiter.length();
918 pos = s.indexOf(delimiter, offset);
919 }
920 }
921
922 return nodeValues.toArray(new String[nodeValues.size()]);
923 }
924
925 public static boolean[] split(String s, boolean x) {
926 return split(s, StringPool.COMMA, x);
927 }
928
929 public static boolean[] split(String s, String delimiter, boolean x) {
930 String[] array = split(s, delimiter);
931 boolean[] newArray = new boolean[array.length];
932
933 for (int i = 0; i < array.length; i++) {
934 boolean value = x;
935
936 try {
937 value = Boolean.valueOf(array[i]).booleanValue();
938 }
939 catch (Exception e) {
940 }
941
942 newArray[i] = value;
943 }
944
945 return newArray;
946 }
947
948 public static double[] split(String s, double x) {
949 return split(s, StringPool.COMMA, x);
950 }
951
952 public static double[] split(String s, String delimiter, double x) {
953 String[] array = split(s, delimiter);
954 double[] newArray = new double[array.length];
955
956 for (int i = 0; i < array.length; i++) {
957 double value = x;
958
959 try {
960 value = Double.parseDouble(array[i]);
961 }
962 catch (Exception e) {
963 }
964
965 newArray[i] = value;
966 }
967
968 return newArray;
969 }
970
971 public static float[] split(String s, float x) {
972 return split(s, StringPool.COMMA, x);
973 }
974
975 public static float[] split(String s, String delimiter, float x) {
976 String[] array = split(s, delimiter);
977 float[] newArray = new float[array.length];
978
979 for (int i = 0; i < array.length; i++) {
980 float value = x;
981
982 try {
983 value = Float.parseFloat(array[i]);
984 }
985 catch (Exception e) {
986 }
987
988 newArray[i] = value;
989 }
990
991 return newArray;
992 }
993
994 public static int[] split(String s, int x) {
995 return split(s, StringPool.COMMA, x);
996 }
997
998 public static int[] split(String s, String delimiter, int x) {
999 String[] array = split(s, delimiter);
1000 int[] newArray = new int[array.length];
1001
1002 for (int i = 0; i < array.length; i++) {
1003 int value = x;
1004
1005 try {
1006 value = Integer.parseInt(array[i]);
1007 }
1008 catch (Exception e) {
1009 }
1010
1011 newArray[i] = value;
1012 }
1013
1014 return newArray;
1015 }
1016
1017 public static long[] split(String s, long x) {
1018 return split(s, StringPool.COMMA, x);
1019 }
1020
1021 public static long[] split(String s, String delimiter, long x) {
1022 String[] array = split(s, delimiter);
1023 long[] newArray = new long[array.length];
1024
1025 for (int i = 0; i < array.length; i++) {
1026 long value = x;
1027
1028 try {
1029 value = Long.parseLong(array[i]);
1030 }
1031 catch (Exception e) {
1032 }
1033
1034 newArray[i] = value;
1035 }
1036
1037 return newArray;
1038 }
1039
1040 public static short[] split(String s, short x) {
1041 return split(s, StringPool.COMMA, x);
1042 }
1043
1044 public static short[] split(String s, String delimiter, short x) {
1045 String[] array = split(s, delimiter);
1046 short[] newArray = new short[array.length];
1047
1048 for (int i = 0; i < array.length; i++) {
1049 short value = x;
1050
1051 try {
1052 value = Short.parseShort(array[i]);
1053 }
1054 catch (Exception e) {
1055 }
1056
1057 newArray[i] = value;
1058 }
1059
1060 return newArray;
1061 }
1062
1063 public static boolean startsWith(String s, char begin) {
1064 return startsWith(s, (new Character(begin)).toString());
1065 }
1066
1067 public static boolean startsWith(String s, String start) {
1068 if ((s == null) || (start == null)) {
1069 return false;
1070 }
1071
1072 if (start.length() > s.length()) {
1073 return false;
1074 }
1075
1076 String temp = s.substring(0, start.length());
1077
1078 if (temp.equalsIgnoreCase(start)) {
1079 return true;
1080 }
1081 else {
1082 return false;
1083 }
1084 }
1085
1086
1096 public static int startsWithWeight(String s1, String s2) {
1097 if ((s1 == null) || (s2 == null)) {
1098 return 0;
1099 }
1100
1101 char[] charArray1 = s1.toCharArray();
1102 char[] charArray2 = s2.toCharArray();
1103
1104 int i = 0;
1105
1106 for (; (i < charArray1.length) && (i < charArray2.length); i++) {
1107 if (charArray1[i] != charArray2[i]) {
1108 break;
1109 }
1110 }
1111
1112 return i;
1113 }
1114
1115 public static String stripBetween(String s, String begin, String end) {
1116 if ((s == null) || (begin == null) || (end == null)) {
1117 return s;
1118 }
1119
1120 StringBuilder sb = new StringBuilder(s.length());
1121
1122 int pos = 0;
1123
1124 while (true) {
1125 int x = s.indexOf(begin, pos);
1126 int y = s.indexOf(end, x + begin.length());
1127
1128 if ((x == -1) || (y == -1)) {
1129 sb.append(s.substring(pos, s.length()));
1130
1131 break;
1132 }
1133 else {
1134 sb.append(s.substring(pos, x));
1135
1136 pos = y + end.length();
1137 }
1138 }
1139
1140 return sb.toString();
1141 }
1142
1143 public static String trim(String s) {
1144 return trim(s, null);
1145 }
1146
1147 public static String trim(String s, char c) {
1148 return trim(s, new char[] {c});
1149 }
1150
1151 public static String trim(String s, char[] exceptions) {
1152 if (s == null) {
1153 return null;
1154 }
1155
1156 char[] charArray = s.toCharArray();
1157
1158 int len = charArray.length;
1159
1160 int x = 0;
1161 int y = charArray.length;
1162
1163 for (int i = 0; i < len; i++) {
1164 char c = charArray[i];
1165
1166 if (_isTrimable(c, exceptions)) {
1167 x = i + 1;
1168 }
1169 else {
1170 break;
1171 }
1172 }
1173
1174 for (int i = len - 1; i >= 0; i--) {
1175 char c = charArray[i];
1176
1177 if (_isTrimable(c, exceptions)) {
1178 y = i;
1179 }
1180 else {
1181 break;
1182 }
1183 }
1184
1185 if ((x != 0) || (y != len)) {
1186 return s.substring(x, y);
1187 }
1188 else {
1189 return s;
1190 }
1191 }
1192
1193 public static String trimLeading(String s) {
1194 return trimLeading(s, null);
1195 }
1196
1197 public static String trimLeading(String s, char c) {
1198 return trimLeading(s, new char[] {c});
1199 }
1200
1201 public static String trimLeading(String s, char[] exceptions) {
1202 if (s == null) {
1203 return null;
1204 }
1205
1206 char[] charArray = s.toCharArray();
1207
1208 int len = charArray.length;
1209
1210 int x = 0;
1211 int y = charArray.length;
1212
1213 for (int i = 0; i < len; i++) {
1214 char c = charArray[i];
1215
1216 if (_isTrimable(c, exceptions)) {
1217 x = i + 1;
1218 }
1219 else {
1220 break;
1221 }
1222 }
1223
1224 if ((x != 0) || (y != len)) {
1225 return s.substring(x, y);
1226 }
1227 else {
1228 return s;
1229 }
1230 }
1231
1232 public static String trimTrailing(String s) {
1233 return trimTrailing(s, null);
1234 }
1235
1236 public static String trimTrailing(String s, char c) {
1237 return trimTrailing(s, new char[] {c});
1238 }
1239
1240 public static String trimTrailing(String s, char[] exceptions) {
1241 if (s == null) {
1242 return null;
1243 }
1244
1245 char[] charArray = s.toCharArray();
1246
1247 int len = charArray.length;
1248
1249 int x = 0;
1250 int y = charArray.length;
1251
1252 for (int i = len - 1; i >= 0; i--) {
1253 char c = charArray[i];
1254
1255 if (_isTrimable(c, exceptions)) {
1256 y = i;
1257 }
1258 else {
1259 break;
1260 }
1261 }
1262
1263 if ((x != 0) || (y != len)) {
1264 return s.substring(x, y);
1265 }
1266 else {
1267 return s;
1268 }
1269 }
1270
1271 public static String upperCase(String s) {
1272 if (s == null) {
1273 return null;
1274 }
1275 else {
1276 return s.toUpperCase();
1277 }
1278 }
1279
1280 public static String upperCaseFirstLetter(String s) {
1281 char[] chars = s.toCharArray();
1282
1283 if ((chars[0] >= 97) && (chars[0] <= 122)) {
1284 chars[0] = (char)(chars[0] - 32);
1285 }
1286
1287 return new String(chars);
1288 }
1289
1290 public static String valueOf(Object obj) {
1291 return String.valueOf(obj);
1292 }
1293
1294 public static String wrap(String text) {
1295 return wrap(text, 80, StringPool.NEW_LINE);
1296 }
1297
1298 public static String wrap(String text, int width, String lineSeparator) {
1299 if (text == null) {
1300 return null;
1301 }
1302
1303 StringBuilder sb = new StringBuilder();
1304
1305 try {
1306 BufferedReader br = new BufferedReader(new StringReader(text));
1307
1308 String s = StringPool.BLANK;
1309
1310 while ((s = br.readLine()) != null) {
1311 if (s.length() == 0) {
1312 sb.append(lineSeparator);
1313 }
1314 else {
1315 String[] tokens = s.split(StringPool.SPACE);
1316 boolean firstWord = true;
1317 int curLineLength = 0;
1318
1319 for (int i = 0; i < tokens.length; i++) {
1320 if (!firstWord) {
1321 sb.append(StringPool.SPACE);
1322 curLineLength++;
1323 }
1324
1325 if (firstWord) {
1326 sb.append(lineSeparator);
1327 }
1328
1329 sb.append(tokens[i]);
1330
1331 curLineLength += tokens[i].length();
1332
1333 if (curLineLength >= width) {
1334 firstWord = true;
1335 curLineLength = 0;
1336 }
1337 else {
1338 firstWord = false;
1339 }
1340 }
1341 }
1342 }
1343 }
1344 catch (IOException ioe) {
1345 _log.error(ioe.getMessage());
1346 }
1347
1348 return sb.toString();
1349 }
1350
1351 private static boolean _isTrimable(char c, char[] exceptions) {
1352 if ((exceptions != null) && (exceptions.length > 0)) {
1353 for (int i = 0; i < exceptions.length; i++) {
1354 if (c == exceptions[i]) {
1355 return false;
1356 }
1357 }
1358 }
1359
1360 return Character.isWhitespace(c);
1361 }
1362
1363 private static Log _log = LogFactoryUtil.getLog(StringUtil.class);
1364
1365}