KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > util > StringUtil


1 /*
2  * $Id: StringUtil.java,v 1.3 2004/12/01 07:54:30 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.util;
15
16 import java.util.StringTokenizer JavaDoc;
17
18 /**
19  * Some string manipulation utilities.
20  *
21  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
22  * @version $Revision: 1.3 $
23  */

24 public class StringUtil {
25     /**
26      * replaces substrings with content 'toFind' with 'replace' in
27      * s and returns the result ('s/$toFind/$replace/g')
28      *
29      * @param s The String the substrings should be replaced in.
30      * @param toFind The substring to be replaced
31      * @param replace The replacement.
32      * @return the string with all replacements.
33      */

34     public static final String JavaDoc replace(String JavaDoc s,
35                                        String JavaDoc toFind, String JavaDoc replace) {
36         StringBuffer JavaDoc erg = new StringBuffer JavaDoc();
37
38         int lastindex = 0;
39         int indexOf = s.indexOf(toFind);
40         if (indexOf == -1) return s;
41         while (indexOf != -1) {
42             erg.append(s.substring(lastindex, indexOf)).append(replace);
43             lastindex = indexOf + toFind.length();
44             indexOf = s.indexOf(toFind, lastindex);
45         }
46
47         erg.append(s.substring(lastindex));
48
49         return erg.toString();
50     }
51
52     /* slower ..
53       ist langsamer, mit jit 1/3, weniger als halb so schnell
54      public static String replaceNew(String s, String toFind, String replace) {
55      StringBuffer erg = new StringBuffer();
56
57      StringTokenizer t = new StringTokenizer(s, toFind);
58      while ( t.hasMoreTokens() ) {
59      erg.append(t.nextToken().trim()).append(replace);
60      }
61      return erg.toString();
62      }
63      */

64
65     /**
66      * replaces all newlines in the given String 's' with the replacement
67      * string 'r'. Each line is trimmed from leading and trailing whitespaces,
68      * then the new line-delimiter is added.
69      *
70      * @param s the source string.
71      * @param r the new line delimiter
72      * @return the resulting string.
73      */

74     public static final String JavaDoc replaceNewLines(String JavaDoc s, String JavaDoc r) {
75         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
76
77         StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(s, "\n");
78         while (t.hasMoreTokens()) {
79             result.append(t.nextToken().trim()).append(r);
80         }
81         return result.toString();
82     }
83
84
85     /**
86      * concatenates two arrays of strings.
87      *
88      * @param s1 the first array of strings.
89      * @param s2 the second array of strings.
90      * @return the resulting array with all strings in s1 and s2
91      */

92     public static final String JavaDoc[] concat(String JavaDoc[] s1, String JavaDoc[] s2) {
93         String JavaDoc[] erg = new String JavaDoc[s1.length + s2.length];
94
95         System.arraycopy(s1, 0, erg, 0, s1.length);
96         System.arraycopy(s2, 0, erg, s1.length, s2.length);
97
98         return erg;
99     }
100
101     private final static char[] ALPHAS = {
102         'a', 'b',
103         'c', 'd', 'e', 'f', 'g', 'h',
104         'i', 'j', 'k', 'l', 'm', 'n',
105         'o', 'p', 'q', 'r', 's', 't',
106         'u', 'v', 'w', 'x', 'y', 'z',
107     };
108
109     /**
110      * All possible digits for representing a number as a String
111      * This is conservative and does not include 'special'
112      * characters since some browsers don't handle them right.
113      * The IE for instance seems to be case insensitive in class
114      * names for CSSs. Grrr.
115      */

116     private final static char[] DIGITS = {
117         '0', '1', '2', '3', '4', '5',
118         '6', '7', '8', '9', 'a', 'b',
119         'c', 'd', 'e', 'f', 'g', 'h',
120         'i', 'j', 'k', 'l', 'm', 'n',
121         'o', 'p', 'q', 'r', 's', 't',
122         'u', 'v', 'w', 'x', 'y', 'z',
123         /* This %@&!-IE is case insensitive for certain
124          * URLs and IDs
125          * 'A' , 'B' ,
126          * 'C' , 'D' , 'E' , 'F' , 'G' , 'H' ,
127          * 'I' , 'J' , 'K' , 'L' , 'M' , 'N' ,
128          * 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' ,
129          * 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z'
130          */

131     };
132
133     public static final int MAX_RADIX = DIGITS.length;
134
135     /**
136      * Codes number up to radix 62.
137      * Note, this method is only public for backward compatiblity. don't
138      * use it.
139      *
140      * @param minDigits returns a string with a least minDigits digits
141      */

142     public static String JavaDoc toString(long i, int radix, int minDigits) {
143         char[] buf = new char[65];
144
145         radix = Math.min(Math.abs(radix), MAX_RADIX);
146         minDigits = Math.min(buf.length - 1, Math.abs(minDigits));
147
148
149         int charPos = buf.length - 1;
150
151         boolean negative = (i < 0);
152         if (negative) {
153             i = -i;
154         }
155
156         while (i >= radix) {
157             buf[charPos--] = DIGITS[(int) (i % radix)];
158             i /= radix;
159         }
160         buf[charPos] = DIGITS[(int) i];
161
162         // if minimum length of the result string is set, pad it with the
163
// zero-representation (that is: '0')
164
while (charPos > buf.length - minDigits)
165             buf[--charPos] = DIGITS[0];
166
167         if (negative) {
168             buf[--charPos] = '-';
169         }
170
171         return new String JavaDoc(buf, charPos, buf.length - charPos);
172     }
173
174     /**
175      * creates a shortest possible string representation of the given
176      * long number that qualifies as an identifier in common programming
177      * languages (and HTML-id's :-)
178      * That is, it must start with a letter.
179      *
180      * @param val the long value to be encoded
181      * @return a string represantation of the given value that qualifies
182      * as an identifier.
183      */

184     public static String JavaDoc toIdentifierString(long val) {
185         char buf[] = new char[14];
186         int i = 0;
187         if (val < 0) {
188             buf[i++] = '_';
189             val = -(val + 1);
190         }
191         buf[i++] = ALPHAS[(int) (val % ALPHAS.length)];
192         val /= ALPHAS.length;
193         while (val != 0 && i < buf.length) {
194             buf[i++] = DIGITS[(int) (val % DIGITS.length)];
195             val /= DIGITS.length;
196         }
197         return new String JavaDoc(buf, 0, i);
198     }
199
200     /**
201      * generates a shortest representation as string for the given long.
202      * This is used to generate session or resource IDs.
203      */

204     public static String JavaDoc toShortestAlphaNumericString(long i) {
205         return toString(i, MAX_RADIX, 0);
206     }
207
208     /**
209      * generates a shortest representation as string for the given with
210      * at least minDigits digits. Unused digits are padded with zero.
211      */

212     public static String JavaDoc toShortestAlphaNumericString(long i, int minDigits) {
213         return toString(i, MAX_RADIX, minDigits);
214     }
215
216     public static String JavaDoc delimitedString(Object JavaDoc[] array) {
217         if (array == null)
218             return null;
219         if (array.length == 0)
220             return "";
221
222         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("" + array[0]);
223         for (int i = 1; i < array.length; i++) {
224             if (array[i] == null)
225                 buffer.append(", null");
226             else {
227                 buffer.append(", ");
228                 buffer.append(array[i]);
229             }
230         }
231         return buffer.toString();
232     }
233
234     /*
235     public static void main(String args[]) {
236         System.out.println(StringUtil.toString(9124, 10, 0));
237
238         System.out.println(StringUtil.toShortestAlphaNumericString(9124));
239         System.out.println(StringUtil.toShortestAlphaNumericString(-9124));
240
241         System.out.println(StringUtil.toString(1, MAX_RADIX, 4));
242
243         System.out.println(StringUtil.toString(9124, MAX_RADIX, 1));
244         System.out.println(StringUtil.toString(9124, MAX_RADIX, 4));
245         System.out.println(StringUtil.toString(9124, MAX_RADIX, 5));
246
247         System.out.println(StringUtil.toString(-1, MAX_RADIX, 64));
248
249         System.out.println("\"" + StringUtil.toShortestAlphaNumericString(3843,
250                                                                           2));
251         for (int i=-10; i < 300; ++i) {
252             System.out.println(toIdentifierString(i));
253         }
254         System.out.println(toIdentifierString(Long.MIN_VALUE));
255         System.out.println(toIdentifierString(Long.MAX_VALUE));
256     }
257     */

258 }
259
260
261
262
263
264
Popular Tags