1 14 package org.wings.util; 15 16 import java.util.StringTokenizer ; 17 18 24 public class StringUtil { 25 34 public static final String replace(String s, 35 String toFind, String replace) { 36 StringBuffer erg = new StringBuffer (); 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 64 65 74 public static final String replaceNewLines(String s, String r) { 75 StringBuffer result = new StringBuffer (); 76 77 StringTokenizer t = new StringTokenizer (s, "\n"); 78 while (t.hasMoreTokens()) { 79 result.append(t.nextToken().trim()).append(r); 80 } 81 return result.toString(); 82 } 83 84 85 92 public static final String [] concat(String [] s1, String [] s2) { 93 String [] erg = new String [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 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 131 }; 132 133 public static final int MAX_RADIX = DIGITS.length; 134 135 142 public static String 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 while (charPos > buf.length - minDigits) 165 buf[--charPos] = DIGITS[0]; 166 167 if (negative) { 168 buf[--charPos] = '-'; 169 } 170 171 return new String (buf, charPos, buf.length - charPos); 172 } 173 174 184 public static String 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 (buf, 0, i); 198 } 199 200 204 public static String toShortestAlphaNumericString(long i) { 205 return toString(i, MAX_RADIX, 0); 206 } 207 208 212 public static String toShortestAlphaNumericString(long i, int minDigits) { 213 return toString(i, MAX_RADIX, minDigits); 214 } 215 216 public static String delimitedString(Object [] array) { 217 if (array == null) 218 return null; 219 if (array.length == 0) 220 return ""; 221 222 StringBuffer buffer = new StringBuffer ("" + 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 258 } 259 260 261 262 263 264 | Popular Tags |