1 16 package org.apache.commons.lang; 17 18 33 public class CharSetUtils { 34 35 42 public CharSetUtils() { 43 } 44 45 68 public static CharSet evaluateSet(String [] set) { 69 if (set == null) { 70 return null; 71 } 72 return new CharSet(set); 73 } 74 75 95 public static String squeeze(String str, String set) { 96 if (StringUtils.isEmpty(str) || StringUtils.isEmpty(set)) { 97 return str; 98 } 99 String [] strs = new String [1]; 100 strs[0] = set; 101 return squeeze(str, strs); 102 } 103 104 118 public static String squeeze(String str, String [] set) { 119 if (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(set)) { 120 return str; 121 } 122 CharSet chars = evaluateSet(set); 123 StringBuffer buffer = new StringBuffer (str.length()); 124 char[] chrs = str.toCharArray(); 125 int sz = chrs.length; 126 char lastChar = ' '; 127 char ch = ' '; 128 for (int i = 0; i < sz; i++) { 129 ch = chrs[i]; 130 if (chars.contains(ch)) { 131 if ((ch == lastChar) && (i != 0)) { 132 continue; 133 } 134 } 135 buffer.append(ch); 136 lastChar = ch; 137 } 138 return buffer.toString(); 139 } 140 141 161 public static int count(String str, String set) { 162 if (StringUtils.isEmpty(str) || StringUtils.isEmpty(set)) { 163 return 0; 164 } 165 String [] strs = new String [1]; 166 strs[0] = set; 167 return count(str, strs); 168 } 169 170 184 public static int count(String str, String [] set) { 185 if (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(set)) { 186 return 0; 187 } 188 CharSet chars = evaluateSet(set); 189 int count = 0; 190 char[] chrs = str.toCharArray(); 191 int sz = chrs.length; 192 for(int i=0; i<sz; i++) { 193 if(chars.contains(chrs[i])) { 194 count++; 195 } 196 } 197 return count; 198 } 199 200 221 public static String keep(String str, String set) { 222 if (str == null) { 223 return null; 224 } 225 if (str.length() == 0 || StringUtils.isEmpty(set)) { 226 return ""; 227 } 228 String [] strs = new String [1]; 229 strs[0] = set; 230 return keep(str, strs); 231 } 232 233 249 public static String keep(String str, String [] set) { 250 if (str == null) { 251 return null; 252 } 253 if (str.length() == 0 || ArrayUtils.isEmpty(set)) { 254 return ""; 255 } 256 return modify(str, set, true); 257 } 258 259 279 public static String delete(String str, String set) { 280 if (StringUtils.isEmpty(str) || StringUtils.isEmpty(set)) { 281 return str; 282 } 283 String [] strs = new String [1]; 284 strs[0] = set; 285 return delete(str, strs); 286 } 287 288 303 public static String delete(String str, String [] set) { 304 if (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(set)) { 305 return str; 306 } 307 return modify(str, set, false); 308 } 309 310 319 private static String modify(String str, String [] set, boolean expect) { 320 CharSet chars = evaluateSet(set); 321 StringBuffer buffer = new StringBuffer (str.length()); 322 char[] chrs = str.toCharArray(); 323 int sz = chrs.length; 324 for(int i=0; i<sz; i++) { 325 if(chars.contains(chrs[i]) == expect) { 326 buffer.append(chrs[i]); 327 } 328 } 329 return buffer.toString(); 330 } 331 332 366 public static String translate(String str, String searchChars, String replaceChars) { 367 if (StringUtils.isEmpty(str)) { 368 return str; 369 } 370 StringBuffer buffer = new StringBuffer (str.length()); 371 char[] chrs = str.toCharArray(); 372 char[] withChrs = replaceChars.toCharArray(); 373 int sz = chrs.length; 374 int withMax = replaceChars.length() - 1; 375 for(int i=0; i<sz; i++) { 376 int idx = searchChars.indexOf(chrs[i]); 377 if(idx != -1) { 378 if(idx > withMax) { 379 idx = withMax; 380 } 381 buffer.append(withChrs[idx]); 382 } else { 383 buffer.append(chrs[i]); 384 } 385 } 386 return buffer.toString(); 387 } 388 389 } 390 | Popular Tags |