1 16 17 package org.springframework.util; 18 19 import java.util.ArrayList ; 20 import java.util.Arrays ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import java.util.Locale ; 26 import java.util.Properties ; 27 import java.util.Set ; 28 import java.util.StringTokenizer ; 29 import java.util.TreeSet ; 30 31 49 public abstract class StringUtils { 50 51 private static final String FOLDER_SEPARATOR = "/"; 52 53 private static final String WINDOWS_FOLDER_SEPARATOR = "\\"; 54 55 private static final String TOP_PATH = ".."; 56 57 private static final String CURRENT_PATH = "."; 58 59 private static final char EXTENSION_SEPARATOR = '.'; 60 61 62 66 79 public static boolean hasLength(String str) { 80 return (str != null && str.length() > 0); 81 } 82 83 99 public static boolean hasText(String str) { 100 if (!hasLength(str)) { 101 return false; 102 } 103 int strLen = str.length(); 104 for (int i = 0; i < strLen; i++) { 105 if (!Character.isWhitespace(str.charAt(i))) { 106 return true; 107 } 108 } 109 return false; 110 } 111 112 119 public static boolean containsWhitespace(String str) { 120 if (!hasLength(str)) { 121 return false; 122 } 123 int strLen = str.length(); 124 for (int i = 0; i < strLen; i++) { 125 if (Character.isWhitespace(str.charAt(i))) { 126 return true; 127 } 128 } 129 return false; 130 } 131 132 138 public static String trimWhitespace(String str) { 139 if (!hasLength(str)) { 140 return str; 141 } 142 StringBuffer buf = new StringBuffer (str); 143 while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) { 144 buf.deleteCharAt(0); 145 } 146 while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) { 147 buf.deleteCharAt(buf.length() - 1); 148 } 149 return buf.toString(); 150 } 151 152 158 public static String trimLeadingWhitespace(String str) { 159 if (!hasLength(str)) { 160 return str; 161 } 162 StringBuffer buf = new StringBuffer (str); 163 while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) { 164 buf.deleteCharAt(0); 165 } 166 return buf.toString(); 167 } 168 169 175 public static String trimTrailingWhitespace(String str) { 176 if (!hasLength(str)) { 177 return str; 178 } 179 StringBuffer buf = new StringBuffer (str); 180 while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) { 181 buf.deleteCharAt(buf.length() - 1); 182 } 183 return buf.toString(); 184 } 185 186 193 public static String trimAllWhitespace(String str) { 194 if (!hasLength(str)) { 195 return str; 196 } 197 StringBuffer buf = new StringBuffer (str); 198 int index = 0; 199 while (buf.length() > index) { 200 if (Character.isWhitespace(buf.charAt(index))) { 201 buf.deleteCharAt(index); 202 } 203 else { 204 index++; 205 } 206 } 207 return buf.toString(); 208 } 209 210 211 218 public static boolean startsWithIgnoreCase(String str, String prefix) { 219 if (str == null || prefix == null) { 220 return false; 221 } 222 if (str.startsWith(prefix)) { 223 return true; 224 } 225 if (str.length() < prefix.length()) { 226 return false; 227 } 228 String lcStr = str.substring(0, prefix.length()).toLowerCase(); 229 String lcPrefix = prefix.toLowerCase(); 230 return lcStr.equals(lcPrefix); 231 } 232 233 240 public static boolean endsWithIgnoreCase(String str, String suffix) { 241 if (str == null || suffix == null) { 242 return false; 243 } 244 if (str.endsWith(suffix)) { 245 return true; 246 } 247 if (str.length() < suffix.length()) { 248 return false; 249 } 250 251 String lcStr = str.substring(str.length() - suffix.length()).toLowerCase(); 252 String lcSuffix = suffix.toLowerCase(); 253 return lcStr.equals(lcSuffix); 254 } 255 256 261 public static int countOccurrencesOf(String str, String sub) { 262 if (str == null || sub == null || str.length() == 0 || sub.length() == 0) { 263 return 0; 264 } 265 int count = 0, pos = 0, idx = 0; 266 while ((idx = str.indexOf(sub, pos)) != -1) { 267 ++count; 268 pos = idx + sub.length(); 269 } 270 return count; 271 } 272 273 281 public static String replace(String inString, String oldPattern, String newPattern) { 282 if (inString == null) { 283 return null; 284 } 285 if (oldPattern == null || newPattern == null) { 286 return inString; 287 } 288 289 StringBuffer sbuf = new StringBuffer (); 290 int pos = 0; int index = inString.indexOf(oldPattern); 293 int patLen = oldPattern.length(); 295 while (index >= 0) { 296 sbuf.append(inString.substring(pos, index)); 297 sbuf.append(newPattern); 298 pos = index + patLen; 299 index = inString.indexOf(oldPattern, pos); 300 } 301 sbuf.append(inString.substring(pos)); 302 303 return sbuf.toString(); 305 } 306 307 311 public static String delete(String inString, String pattern) { 312 return replace(inString, pattern, ""); 313 } 314 315 320 public static String deleteAny(String inString, String charsToDelete) { 321 if (inString == null || charsToDelete == null) { 322 return inString; 323 } 324 StringBuffer out = new StringBuffer (); 325 for (int i = 0; i < inString.length(); i++) { 326 char c = inString.charAt(i); 327 if (charsToDelete.indexOf(c) == -1) { 328 out.append(c); 329 } 330 } 331 return out.toString(); 332 } 333 334 335 339 345 public static String quote(String str) { 346 return (str != null ? "'" + str + "'" : null); 347 } 348 349 356 public static Object quoteIfString(Object obj) { 357 return (obj instanceof String ? quote((String ) obj) : obj); 358 } 359 360 365 public static String unqualify(String qualifiedName) { 366 return unqualify(qualifiedName, '.'); 367 } 368 369 375 public static String unqualify(String qualifiedName, char separator) { 376 return qualifiedName.substring(qualifiedName.lastIndexOf(separator) + 1); 377 } 378 379 386 public static String capitalize(String str) { 387 return changeFirstCharacterCase(str, true); 388 } 389 390 397 public static String uncapitalize(String str) { 398 return changeFirstCharacterCase(str, false); 399 } 400 401 private static String changeFirstCharacterCase(String str, boolean capitalize) { 402 if (str == null || str.length() == 0) { 403 return str; 404 } 405 StringBuffer buf = new StringBuffer (str.length()); 406 if (capitalize) { 407 buf.append(Character.toUpperCase(str.charAt(0))); 408 } 409 else { 410 buf.append(Character.toLowerCase(str.charAt(0))); 411 } 412 buf.append(str.substring(1)); 413 return buf.toString(); 414 } 415 416 422 public static String getFilename(String path) { 423 if (path == null) { 424 return null; 425 } 426 int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR); 427 return (separatorIndex != -1 ? path.substring(separatorIndex + 1) : path); 428 } 429 430 436 public static String getFilenameExtension(String path) { 437 if (path == null) { 438 return null; 439 } 440 int sepIndex = path.lastIndexOf(EXTENSION_SEPARATOR); 441 return (sepIndex != -1 ? path.substring(sepIndex + 1) : null); 442 } 443 444 451 public static String stripFilenameExtension(String path) { 452 if (path == null) { 453 return null; 454 } 455 int sepIndex = path.lastIndexOf(EXTENSION_SEPARATOR); 456 return (sepIndex != -1 ? path.substring(0, sepIndex) : path); 457 } 458 459 467 public static String applyRelativePath(String path, String relativePath) { 468 int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR); 469 if (separatorIndex != -1) { 470 String newPath = path.substring(0, separatorIndex); 471 if (!relativePath.startsWith(FOLDER_SEPARATOR)) { 472 newPath += FOLDER_SEPARATOR; 473 } 474 return newPath + relativePath; 475 } 476 else { 477 return relativePath; 478 } 479 } 480 481 489 public static String cleanPath(String path) { 490 String pathToUse = replace(path, WINDOWS_FOLDER_SEPARATOR, FOLDER_SEPARATOR); 491 492 int prefixIndex = pathToUse.indexOf(":"); 497 String prefix = ""; 498 if (prefixIndex != -1) { 499 prefix = pathToUse.substring(0, prefixIndex + 1); 500 pathToUse = pathToUse.substring(prefixIndex + 1); 501 } 502 503 String [] pathArray = delimitedListToStringArray(pathToUse, FOLDER_SEPARATOR); 504 List pathElements = new LinkedList (); 505 int tops = 0; 506 507 for (int i = pathArray.length - 1; i >= 0; i--) { 508 if (CURRENT_PATH.equals(pathArray[i])) { 509 } 511 else if (TOP_PATH.equals(pathArray[i])) { 512 tops++; 514 } 515 else { 516 if (tops > 0) { 517 tops--; 519 } 520 else { 521 pathElements.add(0, pathArray[i]); 523 } 524 } 525 } 526 527 for (int i = 0; i < tops; i++) { 529 pathElements.add(0, TOP_PATH); 530 } 531 532 return prefix + collectionToDelimitedString(pathElements, FOLDER_SEPARATOR); 533 } 534 535 541 public static boolean pathEquals(String path1, String path2) { 542 return cleanPath(path1).equals(cleanPath(path2)); 543 } 544 545 553 public static Locale parseLocaleString(String localeString) { 554 String [] parts = tokenizeToStringArray(localeString, "_ ", false, false); 555 String language = (parts.length > 0 ? parts[0] : ""); 556 String country = (parts.length > 1 ? parts[1] : ""); 557 String variant = (parts.length > 2 ? parts[2] : ""); 558 return (language.length() > 0 ? new Locale (language, country, variant) : null); 559 } 560 561 562 566 573 public static String [] addStringToArray(String [] array, String str) { 574 if (ObjectUtils.isEmpty(array)) { 575 return new String [] {str}; 576 } 577 String [] newArr = new String [array.length + 1]; 578 System.arraycopy(array, 0, newArr, 0, array.length); 579 newArr[array.length] = str; 580 return newArr; 581 } 582 583 591 public static String [] concatenateStringArrays(String [] array1, String [] array2) { 592 if (ObjectUtils.isEmpty(array1)) { 593 return array2; 594 } 595 if (ObjectUtils.isEmpty(array2)) { 596 return array1; 597 } 598 String [] newArr = new String [array1.length + array2.length]; 599 System.arraycopy(array1, 0, newArr, 0, array1.length); 600 System.arraycopy(array2, 0, newArr, array1.length, array2.length); 601 return newArr; 602 } 603 604 614 public static String [] mergeStringArrays(String [] array1, String [] array2) { 615 if (ObjectUtils.isEmpty(array1)) { 616 return array2; 617 } 618 if (ObjectUtils.isEmpty(array2)) { 619 return array1; 620 } 621 List result = new ArrayList (); 622 result.addAll(Arrays.asList(array1)); 623 for (int i = 0; i < array2.length; i++) { 624 String str = array2[i]; 625 if (!result.contains(str)) { 626 result.add(str); 627 } 628 } 629 return toStringArray(result); 630 } 631 632 637 public static String [] sortStringArray(String [] array) { 638 if (ObjectUtils.isEmpty(array)) { 639 return new String [0]; 640 } 641 Arrays.sort(array); 642 return array; 643 } 644 645 652 public static String [] toStringArray(Collection collection) { 653 if (collection == null) { 654 return null; 655 } 656 return (String []) collection.toArray(new String [collection.size()]); 657 } 658 659 665 public static String [] removeDuplicateStrings(String [] array) { 666 if (ObjectUtils.isEmpty(array)) { 667 return array; 668 } 669 Set set = new TreeSet (); 670 for (int i = 0; i < array.length; i++) { 671 set.add(array[i]); 672 } 673 return toStringArray(set); 674 } 675 676 685 public static String [] split(String toSplit, String delimiter) { 686 if (!hasLength(toSplit) || !hasLength(delimiter)) { 687 return null; 688 } 689 int offset = toSplit.indexOf(delimiter); 690 if (offset < 0) { 691 return null; 692 } 693 String beforeDelimiter = toSplit.substring(0, offset); 694 String afterDelimiter = toSplit.substring(offset + delimiter.length()); 695 return new String [] {beforeDelimiter, afterDelimiter}; 696 } 697 698 709 public static Properties splitArrayElementsIntoProperties(String [] array, String delimiter) { 710 return splitArrayElementsIntoProperties(array, delimiter, null); 711 } 712 713 727 public static Properties splitArrayElementsIntoProperties( 728 String [] array, String delimiter, String charsToDelete) { 729 730 if (ObjectUtils.isEmpty(array)) { 731 return null; 732 } 733 Properties result = new Properties (); 734 for (int i = 0; i < array.length; i++) { 735 String element = array[i]; 736 if (charsToDelete != null) { 737 element = deleteAny(array[i], charsToDelete); 738 } 739 String [] splittedElement = split(element, delimiter); 740 if (splittedElement == null) { 741 continue; 742 } 743 result.setProperty(splittedElement[0].trim(), splittedElement[1].trim()); 744 } 745 return result; 746 } 747 748 763 public static String [] tokenizeToStringArray(String str, String delimiters) { 764 return tokenizeToStringArray(str, delimiters, true, true); 765 } 766 767 786 public static String [] tokenizeToStringArray( 787 String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) { 788 789 if (str == null) { 790 return null; 791 } 792 StringTokenizer st = new StringTokenizer (str, delimiters); 793 List tokens = new ArrayList (); 794 while (st.hasMoreTokens()) { 795 String token = st.nextToken(); 796 if (trimTokens) { 797 token = token.trim(); 798 } 799 if (!ignoreEmptyTokens || token.length() > 0) { 800 tokens.add(token); 801 } 802 } 803 return toStringArray(tokens); 804 } 805 806 817 public static String [] delimitedListToStringArray(String str, String delimiter) { 818 if (str == null) { 819 return new String [0]; 820 } 821 if (delimiter == null) { 822 return new String [] {str}; 823 } 824 List result = new ArrayList (); 825 if ("".equals(delimiter)) { 826 for (int i = 0; i < str.length(); i++) { 827 result.add(str.substring(i, i + 1)); 828 } 829 } 830 else { 831 int pos = 0; 832 int delPos = 0; 833 while ((delPos = str.indexOf(delimiter, pos)) != -1) { 834 result.add(str.substring(pos, delPos)); 835 pos = delPos + delimiter.length(); 836 } 837 if (str.length() > 0 && pos <= str.length()) { 838 result.add(str.substring(pos)); 840 } 841 } 842 return toStringArray(result); 843 } 844 845 850 public static String [] commaDelimitedListToStringArray(String str) { 851 return delimitedListToStringArray(str, ","); 852 } 853 854 860 public static Set commaDelimitedListToSet(String str) { 861 Set set = new TreeSet (); 862 String [] tokens = commaDelimitedListToStringArray(str); 863 for (int i = 0; i < tokens.length; i++) { 864 set.add(tokens[i]); 865 } 866 return set; 867 } 868 869 877 public static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix) { 878 if (CollectionUtils.isEmpty(coll)) { 879 return ""; 880 } 881 StringBuffer sb = new StringBuffer (); 882 Iterator it = coll.iterator(); 883 while (it.hasNext()) { 884 sb.append(prefix).append(it.next()).append(suffix); 885 if (it.hasNext()) { 886 sb.append(delim); 887 } 888 } 889 return sb.toString(); 890 } 891 892 898 public static String collectionToDelimitedString(Collection coll, String delim) { 899 return collectionToDelimitedString(coll, delim, "", ""); 900 } 901 902 907 public static String collectionToCommaDelimitedString(Collection coll) { 908 return collectionToDelimitedString(coll, ","); 909 } 910 911 917 public static String arrayToDelimitedString(Object [] arr, String delim) { 918 if (ObjectUtils.isEmpty(arr)) { 919 return ""; 920 } 921 StringBuffer sb = new StringBuffer (); 922 for (int i = 0; i < arr.length; i++) { 923 if (i > 0) { 924 sb.append(delim); 925 } 926 sb.append(arr[i]); 927 } 928 return sb.toString(); 929 } 930 931 936 public static String arrayToCommaDelimitedString(Object [] arr) { 937 return arrayToDelimitedString(arr, ","); 938 } 939 940 } 941 | Popular Tags |