1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
44   * <a href="StringUtil.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Sandeep Soni
48   * @author Ganesh Ram
49   *
50   */
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         // The problem with using a regexp is that it searches the text in a
279         // case insenstive manner but doens't replace the text in a case
280         // insenstive manner. So the search results actually get messed up. The
281         // best way is to actually parse the results.
282 
283         //return s.replaceAll(
284         //  "(?i)" + keywords, highlight1 + keywords + highlight2);
285 
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         return merge(col.toArray(new Object[col.size()]), delimiter);
457     }
458 
459     public static String merge(Object[] array) {
460         return merge(array, StringPool.COMMA);
461     }
462 
463     public static String merge(Object[] array, String delimiter) {
464         if (array == null) {
465             return null;
466         }
467 
468         StringBuilder sb = new StringBuilder();
469 
470         for (int i = 0; i < array.length; i++) {
471             sb.append(String.valueOf(array[i]).trim());
472 
473             if ((i + 1) != array.length) {
474                 sb.append(delimiter);
475             }
476         }
477 
478         return sb.toString();
479     }
480 
481     public static String randomize(String s) {
482         return Randomizer.getInstance().randomize(s);
483     }
484 
485     public static String read(ClassLoader classLoader, String name)
486         throws IOException {
487 
488         return read(classLoader, name, false);
489     }
490 
491     public static String read(ClassLoader classLoader, String name, boolean all)
492         throws IOException {
493 
494         if (all) {
495             StringBuilder sb = new StringBuilder();
496 
497             Enumeration<URL> enu = classLoader.getResources(name);
498 
499             while (enu.hasMoreElements()) {
500                 URL url = enu.nextElement();
501 
502                 InputStream is = url.openStream();
503 
504                 String s = read(is);
505 
506                 if (s != null) {
507                     sb.append(s);
508                     sb.append(StringPool.NEW_LINE);
509                 }
510 
511                 is.close();
512             }
513 
514             return sb.toString().trim();
515         }
516         else {
517             InputStream is = classLoader.getResourceAsStream(name);
518 
519             String s = read(is);
520 
521             is.close();
522 
523             return s;
524         }
525     }
526 
527     public static String read(InputStream is) throws IOException {
528         StringBuilder sb = new StringBuilder();
529 
530         BufferedReader br = new BufferedReader(new InputStreamReader(is));
531 
532         String line = null;
533 
534         while ((line = br.readLine()) != null) {
535             sb.append(line).append('\n');
536         }
537 
538         br.close();
539 
540         return sb.toString().trim();
541     }
542 
543     public static String remove(String s, String remove) {
544         return remove(s, remove, StringPool.COMMA);
545     }
546 
547     public static String remove(String s, String remove, String delimiter) {
548         if ((s == null) || (remove == null) || (delimiter == null)) {
549             return null;
550         }
551 
552         if (Validator.isNotNull(s) && !s.endsWith(delimiter)) {
553             s += delimiter;
554         }
555 
556         StringBuilder sb = new StringBuilder();
557 
558         sb.append(delimiter);
559         sb.append(remove);
560         sb.append(delimiter);
561 
562         String drd = sb.toString();
563 
564         sb = new StringBuilder();
565 
566         sb.append(remove);
567         sb.append(delimiter);
568 
569         String rd = sb.toString();
570 
571         while (contains(s, remove, delimiter)) {
572             int pos = s.indexOf(drd);
573 
574             if (pos == -1) {
575                 if (s.startsWith(rd)) {
576                     int x = remove.length() + delimiter.length();
577                     int y = s.length();
578 
579                     s = s.substring(x, y);
580                 }
581             }
582             else {
583                 int x = pos + remove.length() + delimiter.length();
584                 int y = s.length();
585 
586                 sb = new StringBuilder();
587 
588                 sb.append(s.substring(0, pos));
589                 sb.append(s.substring(x, y));
590 
591                 s =  sb.toString();
592             }
593         }
594 
595         return s;
596     }
597 
598     public static String replace(String s, char oldSub, char newSub) {
599         if (s == null) {
600             return null;
601         }
602 
603         return s.replace(oldSub, newSub);
604     }
605 
606     public static String replace(String s, char oldSub, String newSub) {
607         if ((s == null) || (newSub == null)) {
608             return null;
609         }
610 
611         // The number 5 is arbitrary and is used as extra padding to reduce
612         // buffer expansion
613 
614         StringBuilder sb = new StringBuilder(s.length() + 5 * newSub.length());
615 
616         char[] charArray = s.toCharArray();
617 
618         for (char c : charArray) {
619             if (c == oldSub) {
620                 sb.append(newSub);
621             }
622             else {
623                 sb.append(c);
624             }
625         }
626 
627         return sb.toString();
628     }
629 
630     public static String replace(String s, String oldSub, String newSub) {
631         if ((s == null) || (oldSub == null) || (newSub == null)) {
632             return null;
633         }
634 
635         int y = s.indexOf(oldSub);
636 
637         if (y >= 0) {
638 
639             // The number 5 is arbitrary and is used as extra padding to reduce
640             // buffer expansion
641 
642             StringBuilder sb = new StringBuilder(
643                 s.length() + 5 * newSub.length());
644 
645             int length = oldSub.length();
646             int x = 0;
647 
648             while (x <= y) {
649                 sb.append(s.substring(x, y));
650                 sb.append(newSub);
651 
652                 x = y + length;
653                 y = s.indexOf(oldSub, x);
654             }
655 
656             sb.append(s.substring(x));
657 
658             return sb.toString();
659         }
660         else {
661             return s;
662         }
663     }
664 
665     public static String replace(String s, String[] oldSubs, String[] newSubs) {
666         if ((s == null) || (oldSubs == null) || (newSubs == null)) {
667             return null;
668         }
669 
670         if (oldSubs.length != newSubs.length) {
671             return s;
672         }
673 
674         for (int i = 0; i < oldSubs.length; i++) {
675             s = replace(s, oldSubs[i], newSubs[i]);
676         }
677 
678         return s;
679     }
680 
681     public static String replace(
682         String s, String[] oldSubs, String[] newSubs, boolean exactMatch) {
683 
684         if ((s == null) || (oldSubs == null) || (newSubs == null)) {
685             return null;
686         }
687 
688         if (oldSubs.length != newSubs.length) {
689             return s;
690         }
691 
692         if (!exactMatch) {
693             replace(s, oldSubs, newSubs);
694         }
695         else {
696             for (int i = 0; i < oldSubs.length; i++) {
697                 s = s.replaceAll("\\b" + oldSubs[i] + "\\b" , newSubs[i]);
698             }
699         }
700 
701         return s;
702     }
703 
704     public static String replaceFirst(String s, char oldSub, char newSub) {
705         if (s == null) {
706             return null;
707         }
708 
709         return s.replaceFirst(String.valueOf(oldSub), String.valueOf(newSub));
710     }
711 
712     public static String replaceFirst(String s, char oldSub, String newSub) {
713         if ((s == null) || (newSub == null)) {
714             return null;
715         }
716 
717         return s.replaceFirst(String.valueOf(oldSub), newSub);
718     }
719 
720     public static String replaceFirst(String s, String oldSub, String newSub) {
721         if ((s == null) || (oldSub == null) || (newSub == null)) {
722             return null;
723         }
724 
725         return s.replaceFirst(oldSub, newSub);
726     }
727 
728     public static String replaceFirst(
729         String s, String[] oldSubs, String[] newSubs) {
730 
731         if ((s == null) || (oldSubs == null) || (newSubs == null)) {
732             return null;
733         }
734 
735         if (oldSubs.length != newSubs.length) {
736             return s;
737         }
738 
739         for (int i = 0; i < oldSubs.length; i++) {
740             s = replaceFirst(s, oldSubs[i], newSubs[i]);
741         }
742 
743         return s;
744     }
745 
746     /**
747      * Returns a string with replaced values. This method will replace all text
748      * in the given string, between the beginning and ending delimiter, with new
749      * values found in the given map. For example, if the string contained the
750      * text <code>[$HELLO$]</code>, and the beginning delimiter was
751      * <code>[$]</code>, and the ending delimiter was <code>$]</code>, and the
752      * values map had a key of <code>HELLO</code> that mapped to
753      * <code>WORLD</code>, then the replaced string will contain the text
754      * <code>[$WORLD$]</code>.
755      *
756      * @param       s the original string
757      * @param       begin the beginning delimiter
758      * @param       end the ending delimiter
759      * @param       values a map of old and new values
760      * @return      a string with replaced values
761      */
762     public static String replaceValues(
763         String s, String begin, String end, Map<String, String> values) {
764 
765         if ((s == null) || (begin == null) || (end == null) ||
766             (values == null) || (values.size() == 0)) {
767 
768             return s;
769         }
770 
771         StringBuilder sb = new StringBuilder(s.length());
772 
773         int pos = 0;
774 
775         while (true) {
776             int x = s.indexOf(begin, pos);
777             int y = s.indexOf(end, x + begin.length());
778 
779             if ((x == -1) || (y == -1)) {
780                 sb.append(s.substring(pos, s.length()));
781 
782                 break;
783             }
784             else {
785                 sb.append(s.substring(pos, x + begin.length()));
786 
787                 String oldValue = s.substring(x + begin.length(), y);
788 
789                 String newValue = values.get(oldValue);
790 
791                 if (newValue == null) {
792                     newValue = oldValue;
793                 }
794 
795                 sb.append(newValue);
796 
797                 pos = y;
798             }
799         }
800 
801         return sb.toString();
802     }
803 
804     public static String reverse(String s) {
805         if (s == null) {
806             return null;
807         }
808 
809         char[] c = s.toCharArray();
810         char[] reverse = new char[c.length];
811 
812         for (int i = 0; i < c.length; i++) {
813             reverse[i] = c[c.length - i - 1];
814         }
815 
816         return new String(reverse);
817     }
818 
819     public static String safePath(String path) {
820         return replace(path, StringPool.DOUBLE_SLASH, StringPool.SLASH);
821     }
822 
823     public static String shorten(String s) {
824         return shorten(s, 20);
825     }
826 
827     public static String shorten(String s, int length) {
828         return shorten(s, length, "...");
829     }
830 
831     public static String shorten(String s, String suffix) {
832         return shorten(s, 20, suffix);
833     }
834 
835     public static String shorten(String s, int length, String suffix) {
836         if ((s == null) || (suffix == null)) {
837             return null;
838         }
839 
840         if (s.length() > length) {
841             for (int j = length; j >= 0; j--) {
842                 if (Character.isWhitespace(s.charAt(j))) {
843                     length = j;
844 
845                     break;
846                 }
847             }
848 
849             StringBuilder sb = new StringBuilder();
850 
851             sb.append(s.substring(0, length));
852             sb.append(suffix);
853 
854             s =  sb.toString();
855         }
856 
857         return s;
858     }
859 
860     public static String[] split(String s) {
861         return split(s, StringPool.COMMA);
862     }
863 
864     public static String[] split(String s, String delimiter) {
865         if (s == null || delimiter == null) {
866             return new String[0];
867         }
868 
869         s = s.trim();
870 
871         if (!s.endsWith(delimiter)) {
872             StringBuilder sb = new StringBuilder();
873 
874             sb.append(s);
875             sb.append(delimiter);
876 
877             s = sb.toString();
878         }
879 
880         if (s.equals(delimiter)) {
881             return new String[0];
882         }
883 
884         List<String> nodeValues = new ArrayList<String>();
885 
886         if (delimiter.equals("\n") || delimiter.equals("\r")) {
887             try {
888                 BufferedReader br = new BufferedReader(new StringReader(s));
889 
890                 String line = null;
891 
892                 while ((line = br.readLine()) != null) {
893                     nodeValues.add(line);
894                 }
895 
896                 br.close();
897             }
898             catch (IOException ioe) {
899                 _log.error(ioe.getMessage());
900             }
901         }
902         else {
903             int offset = 0;
904             int pos = s.indexOf(delimiter, offset);
905 
906             while (pos != -1) {
907                 nodeValues.add(new String(s.substring(offset, pos)));
908 
909                 offset = pos + delimiter.length();
910                 pos = s.indexOf(delimiter, offset);
911             }
912         }
913 
914         return nodeValues.toArray(new String[nodeValues.size()]);
915     }
916 
917     public static boolean[] split(String s, boolean x) {
918         return split(s, StringPool.COMMA, x);
919     }
920 
921     public static boolean[] split(String s, String delimiter, boolean x) {
922         String[] array = split(s, delimiter);
923         boolean[] newArray = new boolean[array.length];
924 
925         for (int i = 0; i < array.length; i++) {
926             boolean value = x;
927 
928             try {
929                 value = Boolean.valueOf(array[i]).booleanValue();
930             }
931             catch (Exception e) {
932             }
933 
934             newArray[i] = value;
935         }
936 
937         return newArray;
938     }
939 
940     public static double[] split(String s, double x) {
941         return split(s, StringPool.COMMA, x);
942     }
943 
944     public static double[] split(String s, String delimiter, double x) {
945         String[] array = split(s, delimiter);
946         double[] newArray = new double[array.length];
947 
948         for (int i = 0; i < array.length; i++) {
949             double value = x;
950 
951             try {
952                 value = Double.parseDouble(array[i]);
953             }
954             catch (Exception e) {
955             }
956 
957             newArray[i] = value;
958         }
959 
960         return newArray;
961     }
962 
963     public static float[] split(String s, float x) {
964         return split(s, StringPool.COMMA, x);
965     }
966 
967     public static float[] split(String s, String delimiter, float x) {
968         String[] array = split(s, delimiter);
969         float[] newArray = new float[array.length];
970 
971         for (int i = 0; i < array.length; i++) {
972             float value = x;
973 
974             try {
975                 value = Float.parseFloat(array[i]);
976             }
977             catch (Exception e) {
978             }
979 
980             newArray[i] = value;
981         }
982 
983         return newArray;
984     }
985 
986     public static int[] split(String s, int x) {
987         return split(s, StringPool.COMMA, x);
988     }
989 
990     public static int[] split(String s, String delimiter, int x) {
991         String[] array = split(s, delimiter);
992         int[] newArray = new int[array.length];
993 
994         for (int i = 0; i < array.length; i++) {
995             int value = x;
996 
997             try {
998                 value = Integer.parseInt(array[i]);
999             }
1000            catch (Exception e) {
1001            }
1002
1003            newArray[i] = value;
1004        }
1005
1006        return newArray;
1007    }
1008
1009    public static long[] split(String s, long x) {
1010        return split(s, StringPool.COMMA, x);
1011    }
1012
1013    public static long[] split(String s, String delimiter, long x) {
1014        String[] array = split(s, delimiter);
1015        long[] newArray = new long[array.length];
1016
1017        for (int i = 0; i < array.length; i++) {
1018            long value = x;
1019
1020            try {
1021                value = Long.parseLong(array[i]);
1022            }
1023            catch (Exception e) {
1024            }
1025
1026            newArray[i] = value;
1027        }
1028
1029        return newArray;
1030    }
1031
1032    public static short[] split(String s, short x) {
1033        return split(s, StringPool.COMMA, x);
1034    }
1035
1036    public static short[] split(String s, String delimiter, short x) {
1037        String[] array = split(s, delimiter);
1038        short[] newArray = new short[array.length];
1039
1040        for (int i = 0; i < array.length; i++) {
1041            short value = x;
1042
1043            try {
1044                value = Short.parseShort(array[i]);
1045            }
1046            catch (Exception e) {
1047            }
1048
1049            newArray[i] = value;
1050        }
1051
1052        return newArray;
1053    }
1054
1055    public static boolean startsWith(String s, char begin) {
1056        return startsWith(s, (new Character(begin)).toString());
1057    }
1058
1059    public static boolean startsWith(String s, String start) {
1060        if ((s == null) || (start == null)) {
1061            return false;
1062        }
1063
1064        if (start.length() > s.length()) {
1065            return false;
1066        }
1067
1068        String temp = s.substring(0, start.length());
1069
1070        if (temp.equalsIgnoreCase(start)) {
1071            return true;
1072        }
1073        else {
1074            return false;
1075        }
1076    }
1077
1078    /**
1079     * Return the number of starting letters that s1 and s2 have in common
1080     * before they deviate.
1081     *
1082     * @param       s1 the first string
1083     * @param       s2 the second string
1084     *
1085     * @return      the number of starting letters that s1 and s2 have in common
1086     *              before they deviate
1087     */
1088    public static int startsWithWeight(String s1, String s2) {
1089        if ((s1 == null) || (s2 == null)) {
1090            return 0;
1091        }
1092
1093        char[] charArray1 = s1.toCharArray();
1094        char[] charArray2 = s2.toCharArray();
1095
1096        int i = 0;
1097
1098        for (; (i < charArray1.length) && (i < charArray2.length); i++) {
1099            if (charArray1[i] != charArray2[i]) {
1100                break;
1101            }
1102        }
1103
1104        return i;
1105    }
1106
1107    public static String stripBetween(String s, String begin, String end) {
1108        if ((s == null) || (begin == null) || (end == null)) {
1109            return s;
1110        }
1111
1112        StringBuilder sb = new StringBuilder(s.length());
1113
1114        int pos = 0;
1115
1116        while (true) {
1117            int x = s.indexOf(begin, pos);
1118            int y = s.indexOf(end, x + begin.length());
1119
1120            if ((x == -1) || (y == -1)) {
1121                sb.append(s.substring(pos, s.length()));
1122
1123                break;
1124            }
1125            else {
1126                sb.append(s.substring(pos, x));
1127
1128                pos = y + end.length();
1129            }
1130        }
1131
1132        return sb.toString();
1133    }
1134
1135    public static String trim(String s) {
1136        return trim(s, null);
1137    }
1138
1139    public static String trim(String s, char c) {
1140        return trim(s, new char[] {c});
1141    }
1142
1143    public static String trim(String s, char[] exceptions) {
1144        if (s == null) {
1145            return null;
1146        }
1147
1148        char[] charArray = s.toCharArray();
1149
1150        int len = charArray.length;
1151
1152        int x = 0;
1153        int y = charArray.length;
1154
1155        for (int i = 0; i < len; i++) {
1156            char c = charArray[i];
1157
1158            if (_isTrimable(c, exceptions)) {
1159                x = i + 1;
1160            }
1161            else {
1162                break;
1163            }
1164        }
1165
1166        for (int i = len - 1; i >= 0; i--) {
1167            char c = charArray[i];
1168
1169            if (_isTrimable(c, exceptions)) {
1170                y = i;
1171            }
1172            else {
1173                break;
1174            }
1175        }
1176
1177        if ((x != 0) || (y != len)) {
1178            return s.substring(x, y);
1179        }
1180        else {
1181            return s;
1182        }
1183    }
1184
1185    public static String trimLeading(String s) {
1186        return trimLeading(s, null);
1187    }
1188
1189    public static String trimLeading(String s, char c) {
1190        return trimLeading(s, new char[] {c});
1191    }
1192
1193    public static String trimLeading(String s, char[] exceptions) {
1194        if (s == null) {
1195            return null;
1196        }
1197
1198        char[] charArray = s.toCharArray();
1199
1200        int len = charArray.length;
1201
1202        int x = 0;
1203        int y = charArray.length;
1204
1205        for (int i = 0; i < len; i++) {
1206            char c = charArray[i];
1207
1208            if (_isTrimable(c, exceptions)) {
1209                x = i + 1;
1210            }
1211            else {
1212                break;
1213            }
1214        }
1215
1216        if ((x != 0) || (y != len)) {
1217            return s.substring(x, y);
1218        }
1219        else {
1220            return s;
1221        }
1222    }
1223
1224    public static String trimTrailing(String s) {
1225        return trimTrailing(s, null);
1226    }
1227
1228    public static String trimTrailing(String s, char c) {
1229        return trimTrailing(s, new char[] {c});
1230    }
1231
1232    public static String trimTrailing(String s, char[] exceptions) {
1233        if (s == null) {
1234            return null;
1235        }
1236
1237        char[] charArray = s.toCharArray();
1238
1239        int len = charArray.length;
1240
1241        int x = 0;
1242        int y = charArray.length;
1243
1244        for (int i = len - 1; i >= 0; i--) {
1245            char c = charArray[i];
1246
1247            if (_isTrimable(c, exceptions)) {
1248                y = i;
1249            }
1250            else {
1251                break;
1252            }
1253        }
1254
1255        if ((x != 0) || (y != len)) {
1256            return s.substring(x, y);
1257        }
1258        else {
1259            return s;
1260        }
1261    }
1262
1263    public static String upperCase(String s) {
1264        if (s == null) {
1265            return null;
1266        }
1267        else {
1268            return s.toUpperCase();
1269        }
1270    }
1271
1272    public static String upperCaseFirstLetter(String s) {
1273        char[] chars = s.toCharArray();
1274
1275        if ((chars[0] >= 97) && (chars[0] <= 122)) {
1276            chars[0] = (char)(chars[0] - 32);
1277        }
1278
1279        return new String(chars);
1280    }
1281
1282    public static String valueOf(Object obj) {
1283        return String.valueOf(obj);
1284    }
1285
1286    public static String wrap(String text) {
1287        return wrap(text, 80, StringPool.NEW_LINE);
1288    }
1289
1290    public static String wrap(String text, int width, String lineSeparator) {
1291        if (text == null) {
1292            return null;
1293        }
1294
1295        StringBuilder sb = new StringBuilder();
1296
1297        try {
1298            BufferedReader br = new BufferedReader(new StringReader(text));
1299
1300            String s = StringPool.BLANK;
1301
1302            while ((s = br.readLine()) != null) {
1303                if (s.length() == 0) {
1304                    sb.append(lineSeparator);
1305                }
1306                else {
1307                    String[] tokens = s.split(StringPool.SPACE);
1308                    boolean firstWord = true;
1309                    int curLineLength = 0;
1310
1311                    for (int i = 0; i < tokens.length; i++) {
1312                        if (!firstWord) {
1313                            sb.append(StringPool.SPACE);
1314                            curLineLength++;
1315                        }
1316
1317                        if (firstWord) {
1318                            sb.append(lineSeparator);
1319                        }
1320
1321                        sb.append(tokens[i]);
1322
1323                        curLineLength += tokens[i].length();
1324
1325                        if (curLineLength >= width) {
1326                            firstWord = true;
1327                            curLineLength = 0;
1328                        }
1329                        else {
1330                            firstWord = false;
1331                        }
1332                    }
1333                }
1334            }
1335        }
1336        catch (IOException ioe) {
1337            _log.error(ioe.getMessage());
1338        }
1339
1340        return sb.toString();
1341    }
1342
1343    private static boolean _isTrimable(char c, char[] exceptions) {
1344        if ((exceptions != null) && (exceptions.length > 0)) {
1345            for (int i = 0; i < exceptions.length; i++) {
1346                if (c == exceptions[i]) {
1347                    return false;
1348                }
1349            }
1350        }
1351
1352        return Character.isWhitespace(c);
1353    }
1354
1355    private static Log _log = LogFactoryUtil.getLog(StringUtil.class);
1356
1357}