1 23 24 29 30 package com.sun.jdo.spi.persistence.utility; 31 32 import java.util.*; 33 34 41 42 public class StringHelper 43 { 44 47 private static final char BACKSLASH = '\\'; 48 private static final char QUOTE = '"'; 49 50 58 public static String arrayToSeparatedList (List list, int beginIndex, 59 int endIndex, String separator) 60 { 61 StringBuffer result = new StringBuffer (); 62 63 if (list != null) 64 { 65 int i, count = (endIndex + 1); 66 67 if ((count > beginIndex) && (list.size() >= count)) 68 result.append(list.get(beginIndex)); 69 70 for (i = beginIndex + 1; i < count; i++) 71 result.append(separator + list.get(i)); 72 } 73 74 return result.toString(); 75 } 76 77 85 public static String arrayToSeparatedList (List list, int beginIndex, 86 int endIndex) 87 { 88 return arrayToSeparatedList(list, beginIndex, endIndex, ","); } 90 91 98 public static String arrayToSeparatedList (List list, String separator) 99 { 100 return arrayToSeparatedList(list, 0, 101 ((list != null) ? (list.size() - 1) : 0), separator); 102 } 103 104 110 public static String arrayToSeparatedList (List list) 111 { 112 return arrayToSeparatedList(list, 0, 113 ((list != null) ? (list.size() - 1) : 0)); 114 } 115 116 121 public static List separatedListToArray (String list, String separator) 122 { 123 ArrayList result = new ArrayList(); 124 125 if (list != null) 126 { 127 StringTokenizer st = new StringTokenizer(list, separator); 128 int i, size = st.countTokens(); 129 130 for (i = 0; i < size; i++) 131 result.add(st.nextToken()); 132 } 133 134 return result; 135 } 136 137 142 public static List separatedListToArray (String list) 143 { 144 return separatedListToArray(list, ","); } 146 147 152 public static String intArrayToSeparatedList(int[] intArray, 153 String separator) 154 { 155 return intArrayToSeparatedList(intArray, 0, 156 ((intArray != null) ? (intArray.length - 1) : 0), separator); 157 } 158 159 166 public static String intArrayToSeparatedList(int[] intArray, int beginIndex, 167 int endIndex, String separator) 168 { 169 StringBuffer result = new StringBuffer (); 170 171 if (intArray != null) 172 { 173 int count = (endIndex + 1); 174 if ((count > beginIndex) && (intArray.length >= count)) 175 result.append(intArray[beginIndex]); 176 177 for (int i = beginIndex + 1; i < count; i++) { 178 result.append(separator); 179 result.append(intArray[i]); 180 } 181 } 182 183 return result.toString(); 184 } 185 186 190 public static boolean isEmpty (String aString) 191 { 192 return ((aString == null) || (aString.trim().length() == 0)); 193 } 194 195 201 public static String getCapitalizedString (String aString) 202 { 203 if (isEmpty(aString)) 204 return aString; 205 206 return Character.toUpperCase(aString.charAt(0)) + aString.substring(1); 207 } 208 209 220 public static String replaceFirst (String string, String oldString, String newString) 221 { 222 int index = string.indexOf(oldString); 223 if (index != -1) { 224 StringBuffer sb = new StringBuffer (string.length()); 225 sb.append(string.substring(0, index)); 226 sb.append(newString); 227 sb.append(string.substring(index + oldString.length())); 228 return sb.toString(); 229 } 230 return string; 231 } 232 233 244 public static String replace (String string, String oldString, 245 String newString) 246 { 247 StringBuffer sb = null; 248 final int l = oldString.length(); 249 int beginIndex = 0; 250 int index; 251 252 while ((index = string.indexOf(oldString, beginIndex)) > -1) 253 { 254 if (sb == null) 256 { 257 sb = new StringBuffer (string.length()); 258 } 259 sb.append(string.substring(beginIndex, index)); 260 sb.append(newString); 261 beginIndex = index + l; 262 } 263 264 if (sb != null) 266 { 267 sb.append(string.substring(beginIndex)); 268 } 269 270 return (sb != null ? sb.toString() : string); 271 } 272 273 279 public static String rtrim (String input) 280 { 281 String retVal = input; 282 283 if (input != null) 284 { 285 int lastCharIndex = input.length() - 1; 286 int originalLastCharIndex = lastCharIndex; 287 288 while ((lastCharIndex >= 0) && 289 Character.isSpaceChar(input.charAt(lastCharIndex))) 290 { 291 lastCharIndex--; 292 } 293 if (lastCharIndex != originalLastCharIndex) 294 { 295 retVal = input.substring(0,lastCharIndex + 1); 297 } 298 } 299 300 return retVal; 301 } 302 303 308 public static String escape(String str) 309 { 310 if (str == null) 311 { 312 return str; 313 } 314 else 315 { 316 int indS = str.indexOf(BACKSLASH); 317 int indQ = str.indexOf(QUOTE); 318 if (indS == -1 && indQ == -1) 319 { 320 return str; 321 } 322 else 323 { 324 StringBuffer buf = new StringBuffer (); 325 char data[] = str.toCharArray(); 326 for (int i = 0; i < data.length; i++) 327 { 328 if (BACKSLASH == data[i] || QUOTE == data[i]) 329 { 330 buf.append(BACKSLASH); 331 } 332 buf.append(data[i]); 333 } 334 return buf.toString(); 335 } 336 } 337 } 338 } 339 | Popular Tags |