Save This Page
Home » Spring-Framework-090522 » org.springframework » util » [javadoc | source]
org.springframework.util
abstract public class: StringUtils [javadoc | source]
java.lang.Object
   org.springframework.util.StringUtils
Miscellaneous String utility methods.

Mainly for internal use within the framework; consider Jakarta's Commons Lang for a more comprehensive suite of String utilities.

This class delivers some simple functionality that should really be provided by the core Java String and StringBuffer classes, such as the ability to #replace all occurrences of a given substring in a target string. It also provides easy-to-use methods to convert between delimited strings, such as CSV strings, and collections and arrays.

Method from org.springframework.util.StringUtils Summary:
addStringToArray,   applyRelativePath,   arrayToCommaDelimitedString,   arrayToDelimitedString,   capitalize,   cleanPath,   collectionToCommaDelimitedString,   collectionToDelimitedString,   collectionToDelimitedString,   commaDelimitedListToSet,   commaDelimitedListToStringArray,   concatenateStringArrays,   containsWhitespace,   containsWhitespace,   countOccurrencesOf,   delete,   deleteAny,   delimitedListToStringArray,   delimitedListToStringArray,   endsWithIgnoreCase,   getFilename,   getFilenameExtension,   hasLength,   hasLength,   hasText,   hasText,   mergeStringArrays,   parseLocaleString,   pathEquals,   quote,   quoteIfString,   removeDuplicateStrings,   replace,   sortStringArray,   split,   splitArrayElementsIntoProperties,   splitArrayElementsIntoProperties,   startsWithIgnoreCase,   stripFilenameExtension,   substringMatch,   toStringArray,   toStringArray,   tokenizeToStringArray,   tokenizeToStringArray,   trimAllWhitespace,   trimArrayElements,   trimLeadingCharacter,   trimLeadingWhitespace,   trimTrailingCharacter,   trimTrailingWhitespace,   trimWhitespace,   uncapitalize,   unqualify,   unqualify
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.springframework.util.StringUtils Detail:
 public static String[] addStringToArray(String[] array,
    String str) 
    Append the given String to the given String array, returning a new array consisting of the input array contents plus the given String.
 public static String applyRelativePath(String path,
    String relativePath) 
    Apply the given relative path to the given path, assuming standard Java folder separation (i.e. "/" separators);
 public static String arrayToCommaDelimitedString(Object[] arr) 
    Convenience method to return a String array as a CSV String. E.g. useful for toString() implementations.
 public static String arrayToDelimitedString(Object[] arr,
    String delim) 
    Convenience method to return a String array as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
 public static String capitalize(String str) 
 public static String cleanPath(String path) 
    Normalize the path by suppressing sequences like "path/.." and inner simple dots.

    The result is convenient for path comparison. For other uses, notice that Windows separators ("\") are replaced by simple slashes.

 public static String collectionToCommaDelimitedString(Collection coll) 
    Convenience method to return a Collection as a CSV String. E.g. useful for toString() implementations.
 public static String collectionToDelimitedString(Collection coll,
    String delim) 
    Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
 public static String collectionToDelimitedString(Collection coll,
    String delim,
    String prefix,
    String suffix) 
    Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
 public static Set commaDelimitedListToSet(String str) 
    Convenience method to convert a CSV string list to a set. Note that this will suppress duplicates.
 public static String[] commaDelimitedListToStringArray(String str) 
    Convert a CSV list into an array of Strings.
 public static String[] concatenateStringArrays(String[] array1,
    String[] array2) 
    Concatenate the given String arrays into one, with overlapping array elements included twice.

    The order of elements in the original arrays is preserved.

 public static boolean containsWhitespace(CharSequence str) 
    Check whether the given CharSequence contains any whitespace characters.
 public static boolean containsWhitespace(String str) 
    Check whether the given String contains any whitespace characters.
 public static int countOccurrencesOf(String str,
    String sub) 
    Count the occurrences of the substring in string s.
 public static String delete(String inString,
    String pattern) 
    Delete all occurrences of the given substring.
 public static String deleteAny(String inString,
    String charsToDelete) 
    Delete any character in a given String.
 public static String[] delimitedListToStringArray(String str,
    String delimiter) 
    Take a String which is a delimited list and convert it to a String array.

    A single delimiter can consists of more than one character: It will still be considered as single delimiter string, rather than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

 public static String[] delimitedListToStringArray(String str,
    String delimiter,
    String charsToDelete) 
    Take a String which is a delimited list and convert it to a String array.

    A single delimiter can consists of more than one character: It will still be considered as single delimiter string, rather than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

 public static boolean endsWithIgnoreCase(String str,
    String suffix) 
    Test if the given String ends with the specified suffix, ignoring upper/lower case.
 public static String getFilename(String path) 
    Extract the filename from the given path, e.g. "mypath/myfile.txt" -> "myfile.txt".
 public static String getFilenameExtension(String path) 
    Extract the filename extension from the given path, e.g. "mypath/myfile.txt" -> "txt".
 public static boolean hasLength(CharSequence str) 
    Check that the given CharSequence is neither null nor of length 0. Note: Will return true for a CharSequence that purely consists of whitespace.

    StringUtils.hasLength(null) = false
    StringUtils.hasLength("") = false
    StringUtils.hasLength(" ") = true
    StringUtils.hasLength("Hello") = true
    
 public static boolean hasLength(String str) 
    Check that the given String is neither null nor of length 0. Note: Will return true for a String that purely consists of whitespace.
 public static boolean hasText(CharSequence str) 
    Check whether the given CharSequence has actual text. More specifically, returns true if the string not null, its length is greater than 0, and it contains at least one non-whitespace character.

    StringUtils.hasText(null) = false
    StringUtils.hasText("") = false
    StringUtils.hasText(" ") = false
    StringUtils.hasText("12345") = true
    StringUtils.hasText(" 12345 ") = true
    
 public static boolean hasText(String str) 
    Check whether the given String has actual text. More specifically, returns true if the string not null, its length is greater than 0, and it contains at least one non-whitespace character.
 public static String[] mergeStringArrays(String[] array1,
    String[] array2) 
    Merge the given String arrays into one, with overlapping array elements only included once.

    The order of elements in the original arrays is preserved (with the exception of overlapping elements, which are only included on their first occurence).

 public static Locale parseLocaleString(String localeString) 
 public static boolean pathEquals(String path1,
    String path2) 
    Compare two paths after normalization of them.
 public static String quote(String str) 
    Quote the given String with single quotes.
 public static Object quoteIfString(Object obj) 
    Turn the given Object into a String with single quotes if it is a String; keeping the Object as-is else.
 public static String[] removeDuplicateStrings(String[] array) 
    Remove duplicate Strings from the given array. Also sorts the array, as it uses a TreeSet.
 public static String replace(String inString,
    String oldPattern,
    String newPattern) 
    Replace all occurences of a substring within a string with another string.
 public static String[] sortStringArray(String[] array) 
    Turn given source String array into sorted array.
 public static String[] split(String toSplit,
    String delimiter) 
    Split a String at the first occurrence of the delimiter. Does not include the delimiter in the result.
 public static Properties splitArrayElementsIntoProperties(String[] array,
    String delimiter) 
    Take an array Strings and split each element based on the given delimiter. A Properties instance is then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the value.

    Will trim both the key and value before adding them to the Properties instance.

 public static Properties splitArrayElementsIntoProperties(String[] array,
    String delimiter,
    String charsToDelete) 
    Take an array Strings and split each element based on the given delimiter. A Properties instance is then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the value.

    Will trim both the key and value before adding them to the Properties instance.

 public static boolean startsWithIgnoreCase(String str,
    String prefix) 
    Test if the given String starts with the specified prefix, ignoring upper/lower case.
 public static String stripFilenameExtension(String path) 
    Strip the filename extension from the given path, e.g. "mypath/myfile.txt" -> "mypath/myfile".
 public static boolean substringMatch(CharSequence str,
    int index,
    CharSequence substring) 
    Test whether the given string matches the given substring at the given index.
 public static String[] toStringArray(Collection collection) 
    Copy the given Collection into a String array. The Collection must contain String elements only.
 public static String[] toStringArray(Enumeration enumeration) 
    Copy the given Enumeration into a String array. The Enumeration must contain String elements only.
 public static String[] tokenizeToStringArray(String str,
    String delimiters) 
    Tokenize the given String into a String array via a StringTokenizer. Trims tokens and omits empty tokens.

    The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray

 public static String[] tokenizeToStringArray(String str,
    String delimiters,
    boolean trimTokens,
    boolean ignoreEmptyTokens) 
    Tokenize the given String into a String array via a StringTokenizer.

    The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray

 public static String trimAllWhitespace(String str) 
    Trim all whitespace from the given String: leading, trailing, and inbetween characters.
 public static String[] trimArrayElements(String[] array) 
    Trim the elements of the given String array, calling String.trim() on each of them.
 public static String trimLeadingCharacter(String str,
    char leadingCharacter) 
    Trim all occurences of the supplied leading character from the given String.
 public static String trimLeadingWhitespace(String str) 
    Trim leading whitespace from the given String.
 public static String trimTrailingCharacter(String str,
    char trailingCharacter) 
    Trim all occurences of the supplied trailing character from the given String.
 public static String trimTrailingWhitespace(String str) 
    Trim trailing whitespace from the given String.
 public static String trimWhitespace(String str) 
    Trim leading and trailing whitespace from the given String.
 public static String uncapitalize(String str) 
 public static String unqualify(String qualifiedName) 
    Unqualify a string qualified by a '.' dot character. For example, "this.name.is.qualified", returns "qualified".
 public static String unqualify(String qualifiedName,
    char separator) 
    Unqualify a string qualified by a separator character. For example, "this:name:is:qualified" returns "qualified" if using a ':' separator.