org.springframework.util
Class StringUtils


public abstract class StringUtils

Miscellaneous string utility methods. 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.

Author:
Rod Johnson
Version: $Id: StringUtils.java,v 1.4 2003/10/09 18:18:41 jhoeller Exp $
Since: 16 April 2001

Method Summary
 static String[]addStringToArray(String[] arr, String s)
           
 static StringarrayToCommaDelimitedString(Object[] arr)
          Convenience method to return a String array as a CSV String.
 static StringarrayToDelimitedString(Object[] arr, String delim)
          Convenience method to return a String array as a delimited (e.g.
 static StringcollectionToCommaDelimitedString(Collection c)
          Convenience method to return a Collection as a CSV String.
 static StringcollectionToDelimitedString(Collection c, String delim)
          Convenience method to return a Collection as a delimited (e.g.
 static SetcommaDelimitedListToSet(String s)
          Convenience method to convert a CSV string list to a set.
 static String[]commaDelimitedListToStringArray(String s)
          Convert a CSV list into an array of Strings.
 static intcountOccurrencesOf(String s, String sub)
          Count the occurrences of the substring in string s
 static Stringdelete(String inString, String pattern)
          Delete all occurrences of the given substring.
 static StringdeleteAny(String inString, String chars)
          Delete any character in a given string.
 static String[]delimitedListToStringArray(String s, String delimiter)
          Take a String which is a delimited list and convert it to a String array
 static Stringreplace(String inString, String oldPattern, String newPattern)
          Replaces all occurences of a substring within a string with another string.
 static String[]tokenizeToStringArray(String s, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
          Tokenize the given String into a String array via a StringTokenizer.

Method Detail

addStringToArray

public static String[] addStringToArray(String[] arr, String s)

arrayToCommaDelimitedString

public static String arrayToCommaDelimitedString(Object[] arr)
Convenience method to return a String array as a CSV String. Useful for toString() implementations.
Parameters:
arr - array to display. Elements may be of any type (toString() will be called on each element).

arrayToDelimitedString

public static String arrayToDelimitedString(Object[] arr, String delim)
Convenience method to return a String array as a delimited (e.g. CSV) String. Useful for toString() implementations
Parameters:
arr - array to display. Elements may be of any type (toString() will be called on each element).
delim - delimiter to use (probably a ,)

collectionToCommaDelimitedString

public static String collectionToCommaDelimitedString(Collection c)
Convenience method to return a Collection as a CSV String. Useful for toString() implementations.
Parameters:
c - Collection to display

collectionToDelimitedString

public static String collectionToDelimitedString(Collection c, String delim)
Convenience method to return a Collection as a delimited (e.g. CSV) String. Useful for toString() implementations.
Parameters:
c - Collection to display
delim - delimiter to use (probably a ",")

commaDelimitedListToSet

public static Set commaDelimitedListToSet(String s)
Convenience method to convert a CSV string list to a set. Note that this will suppress duplicates.
Parameters:
s - CSV String
Returns: a Set of String entries in the list

commaDelimitedListToStringArray

public static String[] commaDelimitedListToStringArray(String s)
Convert a CSV list into an array of Strings.
Parameters:
s - CSV list
Returns: an array of Strings. Returns the empty array if s is null.

countOccurrencesOf

public static int countOccurrencesOf(String s, String sub)
Count the occurrences of the substring in string s
Parameters:
s - string to search in. Returns 0 if this is null
sub - string to search for. Return 0 if this is null.

delete

public static String delete(String inString, String pattern)
Delete all occurrences of the given substring.
Parameters:
pattern - pattern to delete all occurrences of

deleteAny

public static String deleteAny(String inString, String chars)
Delete any character in a given string.
Parameters:
chars - characters to delete e.g. az\n will delete as, zs and new lines

delimitedListToStringArray

public static String[] delimitedListToStringArray(String s, String delimiter)
Take a String which is a delimited list and convert it to a String array
Parameters:
s - String
delimiter - delimiter. This will not be returned
Returns: an array of the tokens in the list

replace

public static String replace(String inString, String oldPattern, String newPattern)
Replaces all occurences of a substring within a string with another string.
Parameters:
inString - String to examine
oldPattern - String to replace
newPattern - String to insert
Returns: a String with the replacements

tokenizeToStringArray

public static String[] tokenizeToStringArray(String s, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
Tokenize the given String into a String array via a StringTokenizer.
Parameters:
s - the String to tokenize
delimiters - the delimiter characters, assembled as String
trimTokens - trim the tokens via String.trim
ignoreEmptyTokens - omit empty tokens from the result array
Returns: an array of the tokens
See Also:
StringTokenizer, java.lang.String.trim()