1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.util;
21
22 import java.io.*;
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.LinkedList;
26 import java.util.StringTokenizer;
27
28 /***
29 * Provides a set of static methods that help with string parsing and modifying.
30 */
31 public class StringHelper
32 {
33
34
35
36 public static final String ALPHABET_LOWER = "abcdefghijklmnopqrstuvwxyz";
37 public static final String ALPHABET_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
38 public static final String NUMBERS_INT = "0123456789";
39 public static final String NUMBERS_DECIMAL = NUMBERS_INT + ".";
40 public static final String MONEY_USD = "$" + NUMBERS_DECIMAL;
41 public static final String ALPHA_NUM
42 = ALPHABET_LOWER + ALPHABET_UPPER + NUMBERS_INT;
43 public static final String EMAIL = ALPHA_NUM + ".@_-";
44 public static final String HOST = ALPHA_NUM + ".";
45 public static final String REGULAR_STRING
46 = EMAIL + "[]§$%///(){}ß?äöü+-*,;.<>|_^°~#";
47 public static final String ANYTHING = null;
48
49
50
51
52
53
54
55 /***
56 * Replaces all occurences of <code>oldChars</code> in <code>s</code> by
57 * <code>newChars</code>.
58 *
59 * @return the modified instance of <code>s</code>
60 */
61 public static String replaceAll(String s, String oldChars, String newChars)
62 {
63 StringBuffer sb = new StringBuffer();
64 int i = 0;
65 while (true) {
66 int j = s.indexOf(oldChars, i);
67 if (j != -1) {
68 sb.append(s.substring(i, j));
69 sb.append(newChars);
70 i = j + oldChars.length();
71 }
72 else {
73 sb.append(s.substring(i));
74 break;
75 }
76 }
77 return sb.toString();
78 }
79
80 /***
81 * Returns a random lower case letter-only string with <code>length</code>
82 * characters.
83 */
84 public static String randomString(int length)
85 {
86 StringBuffer sb = new StringBuffer();
87 for (int i = 0; i < length; i++) {
88 sb.append((char)Math.round(Math.random() * 25 + 97));
89 }
90
91 return sb.toString();
92 }
93
94 /***
95 * Returns a new <code>String</code> that has the first letter of value
96 * set to upper case.
97 */
98 public static String toFirstUpper(String value)
99 {
100 if (value.length() > 1) {
101 return Character.toUpperCase(value.charAt(0)) + value.substring(1);
102 }
103 else {
104 return value.toUpperCase();
105 }
106 }
107
108 /***
109 * Removes all characters from <code>s</code> that are not letters.
110 * Returns a new String which can be for instance used as search text.
111 *
112 * @return a stripped instance of s
113 */
114 public static String stripExtra(String s)
115 {
116 StringBuffer sb = new StringBuffer();
117 boolean newWord = false;
118 char[] chars = s.toCharArray();
119 for (int i = 0; i < chars.length; i++) {
120 if (Character.isLetter(chars[i])) {
121 if (newWord) {
122 sb.append(" ");
123 newWord = false;
124 }
125 sb.append(chars[i]);
126 }
127 else {
128 newWord = true;
129 }
130 }
131
132 return sb.toString();
133 }
134
135 /***
136 * Returns the index of the first digit in <code>s</code>.
137 *
138 * @return -1, if <code>s</code> does not contain digits; the index of
139 * the first digit, otherwise
140 * @see java.lang.String#indexOf(String)
141 */
142 public static int indexOfDigit(String s)
143 {
144 for (int i = 0; i < s.length(); i++) {
145 if (Character.isDigit(s.charAt(i))) {
146 return i;
147 }
148 }
149
150 return -1;
151 }
152
153 /***
154 * Splits <code>text</code> at the first occurence of <code>separator</code>
155 * and returns the left part, excluding the separator.
156 *
157 * @param text the haystack
158 * @param separator the needle
159 * @return the empty string, if no occurence can be found
160 */
161 public static String firstToken(String text, String separator)
162 {
163 if (separator == null) {
164 throw new IllegalArgumentException("separator must not be null");
165 }
166 if (separator.length() == 0) {
167 throw new IllegalArgumentException("separator must not be empty");
168 }
169
170 int i = text.indexOf(separator);
171 return (i == -1) ? text : text.substring(0, i);
172 }
173
174 /***
175 * Splits <code>text</code> at the last occurence of <code>separator</code>
176 * and returns the right part, excluding the separator.
177 *
178 * @param text the haystack
179 * @param separator the needle
180 * @return the empty string, if no occurence can be found
181 */
182 public static String lastToken(String text, String separator)
183 {
184 if (separator == null) {
185 throw new IllegalArgumentException("separator must not be null");
186 }
187 if (separator.length() == 0) {
188 throw new IllegalArgumentException("separator must not be empty");
189 }
190
191 int i = text.lastIndexOf(separator);
192 if (i < 0 || i == text.length() - 1) {
193 return "";
194 }
195 else {
196 return text.substring(i + separator.length(), text.length());
197 }
198 }
199
200 /***
201 * Splits <code>text</code> at the last occurence of <code>separator</code>
202 * and returns the left part, excluding the separator.
203 *
204 * @param text the haystack
205 * @param separator the needle
206 * @return the empty string, if no occurence can be found
207 */
208 public static String lastPrefix(String text, String separator)
209 {
210 if (separator == null) {
211 throw new IllegalArgumentException("separator must not be null");
212 }
213 if (separator.length() == 0) {
214 throw new IllegalArgumentException("separator must not be empty");
215 }
216
217 int i = text.lastIndexOf(separator);
218 return (i == -1) ? text : text.substring(0, i);
219 }
220
221 public static String[] toArray(String value, String separators)
222 {
223 StringTokenizer st = new StringTokenizer(value, separators);
224 String[] array = new String[st.countTokens()];
225 for (int i = 0; i < array.length; i++) {
226 array[i] = st.nextToken();
227 }
228 return array;
229 }
230
231 public static int[] toIntArray(String value, String separators)
232 {
233 StringTokenizer st = new StringTokenizer(value, separators);
234 int[] array = new int[st.countTokens()];
235 for (int i = 0; i < array.length; i++) {
236 try {
237 array[i] = Integer.parseInt(st.nextToken());
238 }
239 catch (NumberFormatException e) {
240 }
241 }
242 return array;
243 }
244
245 public static LinkedList toList(String value, String separators)
246 {
247 StringTokenizer st = new StringTokenizer(value, separators);
248 LinkedList list = new LinkedList();
249
250 while (st.hasMoreTokens()) {
251 list.add(st.nextToken());
252 }
253 return list;
254 }
255
256 /***
257 * Tries to autodect encoding.
258 */
259 public static String toString(byte[] data)
260 {
261 try {
262 return new String(data, "UTF-8");
263 }
264 catch (UnsupportedEncodingException e) {
265 e.printStackTrace(System.err);
266 }
267
268 try {
269 return new String(data, "ISO-8859-1");
270 }
271 catch (UnsupportedEncodingException e) {
272 e.printStackTrace(System.err);
273 }
274
275 return new String(data);
276 }
277
278 public static String toString(int[] array, String separator)
279 {
280 StringBuffer sb = new StringBuffer();
281 for (int i = 0; i < array.length; i++) {
282 sb.append(array[i]);
283 sb.append(separator);
284 }
285 return sb.toString();
286 }
287
288 public static String toString(String[] array, String separator)
289 {
290 StringBuffer sb = new StringBuffer();
291 for (int i = 0; i < array.length; i++) {
292 sb.append(array[i]);
293 sb.append(separator);
294 }
295 return sb.toString();
296 }
297
298 public static String toString(Collection c, String separator)
299 {
300 StringBuffer sb = new StringBuffer();
301 for (Iterator i = c.iterator(); i.hasNext();) {
302 sb.append(i.next().toString());
303 sb.append(separator);
304 }
305 return sb.toString();
306 }
307
308 }