KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > StringUtil


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 import java.lang.reflect.Array JavaDoc;
35
36 /** Provides a collection of convenience methods for processing and
37  * creating objects with <code>String</code> value components.
38  *
39  * @author fredt@users
40  * @author boucherb@users
41  * @version 1.7.2
42  * @since 1.7.0
43  */

44 public class StringUtil {
45
46     /**
47      * Returns a string with non alphanumeric chars converted to the
48      * substitute character. A digit first character is also converted.
49      * By sqlbob@users
50      * @param source string to convert
51      * @param substitute character to use
52      * @return converted string
53      */

54     public static String JavaDoc toLowerSubset(String JavaDoc source, char substitute) {
55
56         int len = source.length();
57         StringBuffer JavaDoc src = new StringBuffer JavaDoc(len);
58         char ch;
59
60         for (int i = 0; i < len; i++) {
61             ch = source.charAt(i);
62
63             if (!Character.isLetterOrDigit(ch)) {
64                 src.append(substitute);
65             } else if ((i == 0) && Character.isDigit(ch)) {
66                 src.append(substitute);
67             } else {
68                 src.append(Character.toLowerCase(ch));
69             }
70         }
71
72         return src.toString();
73     }
74
75     /**
76      * Builds a bracketed CSV list from the array
77      * @param array an array of Objects
78      * @return string
79      */

80     public static String JavaDoc arrayToString(Object JavaDoc array) {
81
82         int len = Array.getLength(array);
83         int last = len - 1;
84         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(2 * (len + 1));
85
86         sb.append('{');
87
88         for (int i = 0; i < len; i++) {
89             sb.append(Array.get(array, i));
90
91             if (i != last) {
92                 sb.append(',');
93             }
94         }
95
96         sb.append('}');
97
98         return sb.toString();
99     }
100
101     /**
102      * Builds a CSV list from the specified String[], separator string and
103      * quote string. <p>
104      *
105      * <ul>
106      * <li>All arguments are assumed to be non-null.
107      * <li>Separates each list element with the value of the
108      * <code>separator</code> argument.
109      * <li>Prepends and appends each element with the value of the
110      * <code>quote</code> argument.
111      * <li> No attempt is made to escape the quote character sequence if it is
112      * found internal to a list element.
113      * <ul>
114      * @return a CSV list
115      * @param separator the <code>String</code> to use as the list element separator
116      * @param quote the <code>String</code> with which to quote the list elements
117      * @param s array of <code>String</code> objects
118      */

119     public static String JavaDoc getList(String JavaDoc[] s, String JavaDoc separator, String JavaDoc quote) {
120
121         int len = s.length;
122         StringBuffer JavaDoc b = new StringBuffer JavaDoc(len * 16);
123
124         for (int i = 0; i < len; i++) {
125             b.append(quote);
126             b.append(s[i]);
127             b.append(quote);
128
129             if (i + 1 < len) {
130                 b.append(separator);
131             }
132         }
133
134         return b.toString();
135     }
136
137     public static String JavaDoc getList(Object JavaDoc[] s, String JavaDoc separator, String JavaDoc quote) {
138
139         int len = s.length;
140         StringBuffer JavaDoc b = new StringBuffer JavaDoc(len * 16);
141
142         for (int i = 0; i < len; i++) {
143             b.append(quote);
144             b.append(s[i]);
145             b.append(quote);
146
147             if (i + 1 < len) {
148                 b.append(separator);
149             }
150         }
151
152         return b.toString();
153     }
154
155     /**
156      * Builds a CSV list from the specified int[], <code>separator</code>
157      * <code>String</code> and <code>quote</code> <code>String</code>. <p>
158      *
159      * <ul>
160      * <li>All arguments are assumed to be non-null.
161      * <li>Separates each list element with the value of the
162      * <code>separator</code> argument.
163      * <li>Prepends and appends each element with the value of the
164      * <code>quote</code> argument.
165      * <ul>
166      * @return a CSV list
167      * @param s the array of int values
168      * @param separator the <code>String</code> to use as the separator
169      * @param quote the <code>String</code> with which to quote the list elements
170      */

171     public static String JavaDoc getList(int[] s, String JavaDoc separator, String JavaDoc quote) {
172
173         int len = s.length;
174         StringBuffer JavaDoc b = new StringBuffer JavaDoc(len * 8);
175
176         for (int i = 0; i < len; i++) {
177             b.append(quote);
178             b.append(s[i]);
179             b.append(quote);
180
181             if (i + 1 < len) {
182                 b.append(separator);
183             }
184         }
185
186         return b.toString();
187     }
188
189     /**
190      * Builds a CSV list from the specified String[][], separator string and
191      * quote string. <p>
192      *
193      * <ul>
194      * <li>All arguments are assumed to be non-null.
195      * <li>Uses only the first element in each subarray.
196      * <li>Separates each list element with the value of the
197      * <code>separator</code> argument.
198      * <li>Prepends and appends each element with the value of the
199      * <code>quote</code> argument.
200      * <li> No attempt is made to escape the quote character sequence if it is
201      * found internal to a list element.
202      * <ul>
203      * @return a CSV list
204      * @param separator the <code>String</code> to use as the list element separator
205      * @param quote the <code>String</code> with which to quote the list elements
206      * @param s the array of <code>String</code> array objects
207      */

208     public static String JavaDoc getList(String JavaDoc[][] s, String JavaDoc separator,
209                                  String JavaDoc quote) {
210
211         int len = s.length;
212         StringBuffer JavaDoc b = new StringBuffer JavaDoc(len * 16);
213
214         for (int i = 0; i < len; i++) {
215             b.append(quote);
216             b.append(s[i][0]);
217             b.append(quote);
218
219             if (i + 1 < len) {
220                 b.append(separator);
221             }
222         }
223
224         return b.toString();
225     }
226
227     /**
228      * Appends a pair of string to the string buffer, using the separator between
229      * and terminator at the end
230      * @param b the buffer
231      * @param s1 first string
232      * @param s2 second string
233      * @param separator separator string
234      * @param terminator terminator string
235      */

236     public static void appendPair(StringBuffer JavaDoc b, String JavaDoc s1, String JavaDoc s2,
237                                   String JavaDoc separator, String JavaDoc terminator) {
238
239         b.append(s1);
240         b.append(separator);
241         b.append(s2);
242         b.append(terminator);
243     }
244
245     /**
246      * Checks if text is empty (characters <= space)
247      * @author: Nitin Chauhan
248      * @return boolean true if text is null or empty, false otherwise
249      * @param s java.lang.String
250      */

251     public static boolean isEmpty(String JavaDoc s) {
252
253         int i = s == null ? 0
254                           : s.length();
255
256         while (i > 0) {
257             if (s.charAt(--i) > ' ') {
258                 return false;
259             }
260         }
261
262         return true;
263     }
264
265     /**
266      * Returns the size of substring that does not contain ane trailing spaces
267      * @param s the string
268      * @return trimmed size
269      */

270     public static int rTrimSize(String JavaDoc s) {
271
272         int i = s.length();
273
274         while (i > 0) {
275             i--;
276
277             if (s.charAt(i) != ' ') {
278                 return i + 1;
279             }
280         }
281
282         return 0;
283     }
284
285     /**
286      * Skips any spaces at or after start and returns the index of first
287      * non-space character;
288      * @param s the string
289      * @param start index to start
290      * @return index of first non-space
291      */

292     public static int skipSpaces(String JavaDoc s, int start) {
293
294         int limit = s.length();
295         int i = start;
296
297         for (; i < limit; i++) {
298             if (s.charAt(i) != ' ') {
299                 break;
300             }
301         }
302
303         return i;
304     }
305
306     /**
307      * Splits the string into an array, using the separator. If separator is
308      * not found in the string, the whole string is returned in the array.
309      *
310      * @param s the string
311      * @param separator the separator
312      * @return array of strings
313      */

314     public static String JavaDoc[] split(String JavaDoc s, String JavaDoc separator) {
315
316         HsqlArrayList list = new HsqlArrayList();
317         int currindex = 0;
318
319         for (boolean more = true; more; ) {
320             int nextindex = s.indexOf(separator, currindex);
321
322             if (nextindex == -1) {
323                 nextindex = s.length();
324                 more = false;
325             }
326
327             list.add(s.substring(currindex, nextindex));
328
329             currindex = nextindex + separator.length();
330         }
331
332         return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
333     }
334 }
335
Popular Tags