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     /**
705      * Returns a string with replaced values. This method will replace all text
706      * in the given string, between the beginning and ending delimiter, with new
707      * values found in the given map. For example, if the string contained the
708      * text <code>[$HELLO$]</code>, and the beginning delimiter was
709      * <code>[$]</code>, and the ending delimiter was <code>$]</code>, and the
710      * values map had a key of <code>HELLO</code> that mapped to
711      * <code>WORLD</code>, then the replaced string will contain the text
712      * <code>[$WORLD$]</code>.
713      *
714      * @param       s the original string
715      * @param       begin the beginning delimiter
716      * @param       end the ending delimiter
717      * @param       values a map of old and new values
718      * @return      a string with replaced values
719      */
720     public static String replaceValues(
721         String s, String begin, String end, Map<String, String> values) {
722 
723         if ((s == null) || (begin == null) || (end == null) ||
724             (values == null) || (values.size() == 0)) {
725 
726             return s;
727         }
728 
729         StringBuilder sb = new StringBuilder(s.length());
730 
731         int pos = 0;
732 
733         while (true) {
734             int x = s.indexOf(begin, pos);
735             int y = s.indexOf(end, x + begin.length());
736 
737             if ((x == -1) || (y == -1)) {
738                 sb.append(s.substring(pos, s.length()));
739 
740                 break;
741             }
742             else {
743                 sb.append(s.substring(pos, x + begin.length()));
744 
745                 String oldValue = s.substring(x + begin.length(), y);
746 
747                 String newValue = values.get(oldValue);
748 
749                 if (newValue == null) {
750                     newValue = oldValue;
751                 }
752 
753                 sb.append(newValue);
754 
755                 pos = y;
756             }
757         }
758 
759         return sb.toString();
760     }
761 
762     public static String reverse(String s) {
763         if (s == null) {
764             return null;
765         }
766 
767         char[] c = s.toCharArray();
768         char[] reverse = new char[c.length];
769 
770         for (int i = 0; i < c.length; i++) {
771             reverse[i] = c[c.length - i - 1];
772         }
773 
774         return new String(reverse);
775     }
776 
777     public static String safePath(String path) {
778         return replace(path, StringPool.DOUBLE_SLASH, StringPool.SLASH);
779     }
780 
781     public static String shorten(String s) {
782         return shorten(s, 20);
783     }
784 
785     public static String shorten(String s, int length) {
786         return shorten(s, length, "...");
787     }
788 
789     public static String shorten(String s, String suffix) {
790         return shorten(s, 20, suffix);
791     }
792 
793     public static String shorten(String s, int length, String suffix) {
794         if ((s == null) || (suffix == null)) {
795             return null;
796         }
797 
798         if (s.length() > length) {
799             for (int j = length; j >= 0; j--) {
800                 if (Character.isWhitespace(s.charAt(j))) {
801                     length = j;
802 
803                     break;
804                 }
805             }
806 
807             StringBuilder sb = new StringBuilder();
808 
809             sb.append(s.substring(0, length));
810             sb.append(suffix);
811 
812             s =  sb.toString();
813         }
814 
815         return s;
816     }
817 
818     public static String[] split(String s) {
819         return split(s, StringPool.COMMA);
820     }
821 
822     public static String[] split(String s, String delimiter) {
823         if (s == null || delimiter == null) {
824             return new String[0];
825         }
826 
827         s = s.trim();
828 
829         if (!s.endsWith(delimiter)) {
830             StringBuilder sb = new StringBuilder();
831 
832             sb.append(s);
833             sb.append(delimiter);
834 
835             s = sb.toString();
836         }
837 
838         if (s.equals(delimiter)) {
839             return new String[0];
840         }
841 
842         List<String> nodeValues = new ArrayList<String>();
843 
844         if (delimiter.equals("\n") || delimiter.equals("\r")) {
845             try {
846                 BufferedReader br = new BufferedReader(new StringReader(s));
847 
848                 String line = null;
849 
850                 while ((line = br.readLine()) != null) {
851                     nodeValues.add(line);
852                 }
853 
854                 br.close();
855             }
856             catch (IOException ioe) {
857                 _log.error(ioe.getMessage());
858             }
859         }
860         else {
861             int offset = 0;
862             int pos = s.indexOf(delimiter, offset);
863 
864             while (pos != -1) {
865                 nodeValues.add(new String(s.substring(offset, pos)));
866 
867                 offset = pos + delimiter.length();
868                 pos = s.indexOf(delimiter, offset);
869             }
870         }
871 
872         return nodeValues.toArray(new String[nodeValues.size()]);
873     }
874 
875     public static boolean[] split(String s, boolean x) {
876         return split(s, StringPool.COMMA, x);
877     }
878 
879     public static boolean[] split(String s, String delimiter, boolean x) {
880         String[] array = split(s, delimiter);
881         boolean[] newArray = new boolean[array.length];
882 
883         for (int i = 0; i < array.length; i++) {
884             boolean value = x;
885 
886             try {
887                 value = Boolean.valueOf(array[i]).booleanValue();
888             }
889             catch (Exception e) {
890             }
891 
892             newArray[i] = value;
893         }
894 
895         return newArray;
896     }
897 
898     public static double[] split(String s, double x) {
899         return split(s, StringPool.COMMA, x);
900     }
901 
902     public static double[] split(String s, String delimiter, double x) {
903         String[] array = split(s, delimiter);
904         double[] newArray = new double[array.length];
905 
906         for (int i = 0; i < array.length; i++) {
907             double value = x;
908 
909             try {
910                 value = Double.parseDouble(array[i]);
911             }
912             catch (Exception e) {
913             }
914 
915             newArray[i] = value;
916         }
917 
918         return newArray;
919     }
920 
921     public static float[] split(String s, float x) {
922         return split(s, StringPool.COMMA, x);
923     }
924 
925     public static float[] split(String s, String delimiter, float x) {
926         String[] array = split(s, delimiter);
927         float[] newArray = new float[array.length];
928 
929         for (int i = 0; i < array.length; i++) {
930             float value = x;
931 
932             try {
933                 value = Float.parseFloat(array[i]);
934             }
935             catch (Exception e) {
936             }
937 
938             newArray[i] = value;
939         }
940 
941         return newArray;
942     }
943 
944     public static int[] split(String s, int x) {
945         return split(s, StringPool.COMMA, x);
946     }
947 
948     public static int[] split(String s, String delimiter, int x) {
949         String[] array = split(s, delimiter);
950         int[] newArray = new int[array.length];
951 
952         for (int i = 0; i < array.length; i++) {
953             int value = x;
954 
955             try {
956                 value = Integer.parseInt(array[i]);
957             }
958             catch (Exception e) {
959             }
960 
961             newArray[i] = value;
962         }
963 
964         return newArray;
965     }
966 
967     public static long[] split(String s, long x) {
968         return split(s, StringPool.COMMA, x);
969     }
970 
971     public static long[] split(String s, String delimiter, long x) {
972         String[] array = split(s, delimiter);
973         long[] newArray = new long[array.length];
974 
975         for (int i = 0; i < array.length; i++) {
976             long value = x;
977 
978             try {
979                 value = Long.parseLong(array[i]);
980             }
981             catch (Exception e) {
982             }
983 
984             newArray[i] = value;
985         }
986 
987         return newArray;
988     }
989 
990     public static short[] split(String s, short x) {
991         return split(s, StringPool.COMMA, x);
992     }
993 
994     public static short[] split(String s, String delimiter, short x) {
995         String[] array = split(s, delimiter);
996         short[] newArray = new short[array.length];
997 
998         for (int i = 0; i < array.length; i++) {
999             short value = x;
1000
1001            try {
1002                value = Short.parseShort(array[i]);
1003            }
1004            catch (Exception e) {
1005            }
1006
1007            newArray[i] = value;
1008        }
1009
1010        return newArray;
1011    }
1012
1013    public static boolean startsWith(String s, char begin) {
1014        return startsWith(s, (new Character(begin)).toString());
1015    }
1016
1017    public static boolean startsWith(String s, String start) {
1018        if ((s == null) || (start == null)) {
1019            return false;
1020        }
1021
1022        if (start.length() > s.length()) {
1023            return false;
1024        }
1025
1026        String temp = s.substring(0, start.length());
1027
1028        if (temp.equalsIgnoreCase(start)) {
1029            return true;
1030        }
1031        else {
1032            return false;
1033        }
1034    }
1035
1036    /**
1037     * Return the number of starting letters that s1 and s2 have in common
1038     * before they deviate.
1039     *
1040     * @param       s1 the first string
1041     * @param       s2 the second string
1042     *
1043     * @return      the number of starting letters that s1 and s2 have in common
1044     *              before they deviate
1045     */
1046    public static int startsWithWeight(String s1, String s2) {
1047        if ((s1 == null) || (s2 == null)) {
1048            return 0;
1049        }
1050
1051        char[] charArray1 = s1.toCharArray();
1052        char[] charArray2 = s2.toCharArray();
1053
1054        int i = 0;
1055
1056        for (; (i < charArray1.length) && (i < charArray2.length); i++) {
1057            if (charArray1[i] != charArray2[i]) {
1058                break;
1059            }
1060        }
1061
1062        return i;
1063    }
1064
1065    public static String stripBetween(String s, String begin, String end) {
1066        if ((s == null) || (begin == null) || (end == null)) {
1067            return s;
1068        }
1069
1070        StringBuilder sb = new StringBuilder(s.length());
1071
1072        int pos = 0;
1073
1074        while (true) {
1075            int x = s.indexOf(begin, pos);
1076            int y = s.indexOf(end, x + begin.length());
1077
1078            if ((x == -1) || (y == -1)) {
1079                sb.append(s.substring(pos, s.length()));
1080
1081                break;
1082            }
1083            else {
1084                sb.append(s.substring(pos, x));
1085
1086                pos = y + end.length();
1087            }
1088        }
1089
1090        return sb.toString();
1091    }
1092
1093    public static String trim(String s) {
1094        return trim(s, null);
1095    }
1096
1097    public static String trim(String s, char c) {
1098        return trim(s, new char[] {c});
1099    }
1100
1101    public static String trim(String s, char[] exceptions) {
1102        if (s == null) {
1103            return null;
1104        }
1105
1106        char[] charArray = s.toCharArray();
1107
1108        int len = charArray.length;
1109
1110        int x = 0;
1111        int y = charArray.length;
1112
1113        for (int i = 0; i < len; i++) {
1114            char c = charArray[i];
1115
1116            if (_isTrimable(c, exceptions)) {
1117                x = i + 1;
1118            }
1119            else {
1120                break;
1121            }
1122        }
1123
1124        for (int i = len - 1; i >= 0; i--) {
1125            char c = charArray[i];
1126
1127            if (_isTrimable(c, exceptions)) {
1128                y = i;
1129            }
1130            else {
1131                break;
1132            }
1133        }
1134
1135        if ((x != 0) || (y != len)) {
1136            return s.substring(x, y);
1137        }
1138        else {
1139            return s;
1140        }
1141    }
1142
1143    public static String trimLeading(String s) {
1144        return trimLeading(s, null);
1145    }
1146
1147    public static String trimLeading(String s, char c) {
1148        return trimLeading(s, new char[] {c});
1149    }
1150
1151    public static String trimLeading(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        if ((x != 0) || (y != len)) {
1175            return s.substring(x, y);
1176        }
1177        else {
1178            return s;
1179        }
1180    }
1181
1182    public static String trimTrailing(String s) {
1183        return trimTrailing(s, null);
1184    }
1185
1186    public static String trimTrailing(String s, char c) {
1187        return trimTrailing(s, new char[] {c});
1188    }
1189
1190    public static String trimTrailing(String s, char[] exceptions) {
1191        if (s == null) {
1192            return null;
1193        }
1194
1195        char[] charArray = s.toCharArray();
1196
1197        int len = charArray.length;
1198
1199        int x = 0;
1200        int y = charArray.length;
1201
1202        for (int i = len - 1; i >= 0; i--) {
1203            char c = charArray[i];
1204
1205            if (_isTrimable(c, exceptions)) {
1206                y = i;
1207            }
1208            else {
1209                break;
1210            }
1211        }
1212
1213        if ((x != 0) || (y != len)) {
1214            return s.substring(x, y);
1215        }
1216        else {
1217            return s;
1218        }
1219    }
1220
1221    public static String upperCase(String s) {
1222        if (s == null) {
1223            return null;
1224        }
1225        else {
1226            return s.toUpperCase();
1227        }
1228    }
1229
1230    public static String upperCaseFirstLetter(String s) {
1231        char[] chars = s.toCharArray();
1232
1233        if ((chars[0] >= 97) && (chars[0] <= 122)) {
1234            chars[0] = (char)(chars[0] - 32);
1235        }
1236
1237        return new String(chars);
1238    }
1239
1240    public static String wrap(String text) {
1241        return wrap(text, 80, StringPool.NEW_LINE);
1242    }
1243
1244    public static String wrap(String text, int width, String lineSeparator) {
1245        if (text == null) {
1246            return null;
1247        }
1248
1249        StringBuilder sb = new StringBuilder();
1250
1251        try {
1252            BufferedReader br = new BufferedReader(new StringReader(text));
1253
1254            String s = StringPool.BLANK;
1255
1256            while ((s = br.readLine()) != null) {
1257                if (s.length() == 0) {
1258                    sb.append(lineSeparator);
1259                }
1260                else {
1261                    String[] tokens = s.split(StringPool.SPACE);
1262                    boolean firstWord = true;
1263                    int curLineLength = 0;
1264
1265                    for (int i = 0; i < tokens.length; i++) {
1266                        if (!firstWord) {
1267                            sb.append(StringPool.SPACE);
1268                            curLineLength++;
1269                        }
1270
1271                        if (firstWord) {
1272                            sb.append(lineSeparator);
1273                        }
1274
1275                        sb.append(tokens[i]);
1276
1277                        curLineLength += tokens[i].length();
1278
1279                        if (curLineLength >= width) {
1280                            firstWord = true;
1281                            curLineLength = 0;
1282                        }
1283                        else {
1284                            firstWord = false;
1285                        }
1286                    }
1287                }
1288            }
1289        }
1290        catch (IOException ioe) {
1291            _log.error(ioe.getMessage());
1292        }
1293
1294        return sb.toString();
1295    }
1296
1297    private static boolean _isTrimable(char c, char[] exceptions) {
1298        if ((exceptions != null) && (exceptions.length > 0)) {
1299            for (int i = 0; i < exceptions.length; i++) {
1300                if (c == exceptions[i]) {
1301                    return false;
1302                }
1303            }
1304        }
1305
1306        return Character.isWhitespace(c);
1307    }
1308
1309    private static Log _log = LogFactoryUtil.getLog(StringUtil.class);
1310
1311}