1 30 31 32 package org.hsqldb.lib; 33 34 import java.lang.reflect.Array ; 35 36 44 public class StringUtil { 45 46 54 public static String toLowerSubset(String source, char substitute) { 55 56 int len = source.length(); 57 StringBuffer src = new StringBuffer (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 80 public static String arrayToString(Object array) { 81 82 int len = Array.getLength(array); 83 int last = len - 1; 84 StringBuffer sb = new StringBuffer (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 119 public static String getList(String [] s, String separator, String quote) { 120 121 int len = s.length; 122 StringBuffer b = new StringBuffer (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 getList(Object [] s, String separator, String quote) { 138 139 int len = s.length; 140 StringBuffer b = new StringBuffer (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 171 public static String getList(int[] s, String separator, String quote) { 172 173 int len = s.length; 174 StringBuffer b = new StringBuffer (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 208 public static String getList(String [][] s, String separator, 209 String quote) { 210 211 int len = s.length; 212 StringBuffer b = new StringBuffer (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 236 public static void appendPair(StringBuffer b, String s1, String s2, 237 String separator, String terminator) { 238 239 b.append(s1); 240 b.append(separator); 241 b.append(s2); 242 b.append(terminator); 243 } 244 245 251 public static boolean isEmpty(String 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 270 public static int rTrimSize(String 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 292 public static int skipSpaces(String 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 314 public static String [] split(String s, String 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 []) list.toArray(new String [list.size()]); 333 } 334 } 335 | Popular Tags |