KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > util > StringUtils


1 package org.ashkelon.util;
2 /**
3  * Copyright UptoData Inc. 2001
4  * March 2001
5  */

6
7 import java.util.ArrayList JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import org.apache.oro.text.regex.MalformedPatternException;
11 import org.apache.oro.text.regex.Perl5Compiler;
12 import org.apache.oro.text.regex.Perl5Matcher;
13 import org.apache.oro.text.regex.Perl5Substitution;
14 import org.apache.oro.text.regex.Util;
15
16
17 /**
18  * Various string utilities. Mainly split & join.
19  *
20  * @author Eitan Suez
21  */

22 public class StringUtils
23 {
24    public static boolean isBlank(java.lang.String JavaDoc text)
25    {
26       return (text == null || text.trim().equals(""));
27    }
28    
29    public static String JavaDoc avoidNull(String JavaDoc text)
30    {
31       return (text==null) ? "" : text;
32    }
33    
34    public static String JavaDoc wrap(String JavaDoc text, String JavaDoc wrap)
35    {
36       return wrap + text + wrap;
37    }
38
39    /**
40     * analog to perl or javascript join() method
41     */

42    public static String JavaDoc join(Object JavaDoc[] text, String JavaDoc concatenator)
43    {
44       if (text.length == 0) { return ""; }
45       String JavaDoc result = (text[0] == null) ? "" : (String JavaDoc) text[0];
46       for (int i=1; i<text.length; i++)
47       {
48          result += concatenator + ((text[i] == null) ? "" : (String JavaDoc) text[i]);
49       }
50       return result;
51    }
52    
53    /**
54     * produce text x numtimes, concatenated by concatenator text
55     * e.g. "123" x 3 with concat = "," procudes: "123,123,123"
56     */

57    public static String JavaDoc join(String JavaDoc text, String JavaDoc concatenator, int numtimes)
58    {
59       if(text==null) { return ""; }
60       if(numtimes <= 0) { return ""; }
61       String JavaDoc result = text;
62       for (int i=1; i<numtimes; i++)
63       {
64          result += concatenator + text;
65       }
66       return result;
67    }
68    
69    /**
70     * the analog of perl's or javascript's split() method
71     */

72    public static String JavaDoc[] split(String JavaDoc text, String JavaDoc delimiter)
73    {
74       ArrayList JavaDoc list = new ArrayList JavaDoc(10);
75       StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(text, delimiter);
76       while (stk.hasMoreTokens())
77       {
78          list.add(stk.nextToken().trim());
79       }
80       String JavaDoc[] results = new String JavaDoc[list.size()];
81       list.toArray(results);
82       return results;
83    }
84    
85    public static void main(String JavaDoc args[])
86    {
87       String JavaDoc[] list = StringUtils.split("eitan", ",");
88       for (int i=0; i<list.length; i++)
89       {
90          Logger.getInstance().traceln(list[i]);
91       }
92    }
93
94    
95    public static boolean getCommandLineOption(String JavaDoc option, String JavaDoc[][] options)
96    {
97       boolean value = false;
98       for (int i = 0; i < options.length; i++)
99       {
100          String JavaDoc[] opt = options[i];
101          if (opt[0].equals(option))
102          {
103             value = true;
104          }
105       }
106       return value;
107    }
108    
109    public static int getCommandLineOption(String JavaDoc option, String JavaDoc[][] options, int defaultValue)
110    {
111       int value = defaultValue;
112       for (int i = 0; i < options.length; i++)
113       {
114          String JavaDoc[] opt = options[i];
115          if (opt[0].equals(option))
116          {
117             try
118             {
119                value = Integer.parseInt(opt[1]);
120             } catch (NumberFormatException JavaDoc ex)
121             {
122                value = defaultValue;
123             }
124          }
125       }
126       return value;
127    }
128    
129    public static String JavaDoc getStringCommandLineOption(String JavaDoc option, String JavaDoc[][] options)
130    {
131       for (int i = 0; i < options.length; i++)
132       {
133          String JavaDoc[] opt = options[i];
134          if (opt[0].equals(option))
135          {
136             return opt[1];
137          }
138       }
139       return "";
140    }
141    
142    
143    public static String JavaDoc truncate(String JavaDoc text, int length)
144    {
145       if (text.length() > length)
146       {
147          StringBuffer JavaDoc buff = new StringBuffer JavaDoc(text);
148          buff.setLength(length);
149          return buff.toString();
150       } // else..
151
return text;
152    }
153
154    public static String JavaDoc substitute(String JavaDoc input, String JavaDoc pattern, String JavaDoc replacement)
155    {
156      try
157      {
158         return Util.substitute(new Perl5Matcher(),
159                                new Perl5Compiler().compile(pattern),
160                                new Perl5Substitution(replacement),
161                                input, Util.SUBSTITUTE_ALL);
162      }
163      catch (MalformedPatternException ex)
164      {
165        return input;
166      }
167    }
168
169
170   /**
171    * strips out any null characters from string
172    * @return string less any null characters in it
173    */

174   public static String JavaDoc stripNull(String JavaDoc input)
175   {
176     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
177     char c;
178     for (int i=0; i<input.length(); i++)
179     {
180       c = input.charAt(i);
181       if (c != 0)
182       {
183         result.append(c);
184       }
185     }
186     return result.toString();
187   }
188
189 }
190
Popular Tags