com.interface21.util
Class StringUtils

java.lang.Object
  |
  +--com.interface21.util.StringUtils

public abstract class StringUtils
extends java.lang.Object

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.

Since:
16 April 2001
Author:
Rod Johnson

Constructor Summary
StringUtils()
           
 
Method Summary
static java.lang.String arrayToDelimitedString(java.lang.Object[] arr, java.lang.String delim)
          Convenience method to return a String array as a delimited (e.g.
static java.lang.String collectionToDelimitedString(java.util.Collection c, java.lang.String delim)
          Convenience method to return a Collection as a delimited (e.g.
static java.util.Set commaDelimitedListToSet(java.lang.String s)
          Convenience method to convert a CSV string list to a set.
static java.lang.String[] commaDelimitedListToStringArray(java.lang.String s)
          Convert a CSV list into an array of Strings
static int countOccurrencesOf(java.lang.String s, java.lang.String sub)
          Count the occurrences of the substring in string s
static java.lang.String delete(java.lang.String inString, java.lang.String pattern)
          Delete all occurrences of the given substring.
static java.lang.String deleteAny(java.lang.String inString, java.lang.String chars)
          Delete any character in a given string.
static java.lang.String[] delimitedListToStringArray(java.lang.String s, java.lang.String delimiter)
          Take a String which is a delimited list and convert it to a String array
static java.lang.String iteratorToDelimitedString(java.util.Iterator itr, java.lang.String delim)
          Convenience method to return a Collection as a delimited (e.g.
static java.lang.String replace(java.lang.String inString, java.lang.String oldPattern, java.lang.String newPattern)
          Replaces all occurences of a substring within a string with another string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtils

public StringUtils()
Method Detail

countOccurrencesOf

public static int countOccurrencesOf(java.lang.String s,
                                     java.lang.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.

replace

public static java.lang.String replace(java.lang.String inString,
                                       java.lang.String oldPattern,
                                       java.lang.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

delete

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

deleteAny

public static java.lang.String deleteAny(java.lang.String inString,
                                         java.lang.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 java.lang.String[] delimitedListToStringArray(java.lang.String s,
                                                            java.lang.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

commaDelimitedListToStringArray

public static java.lang.String[] commaDelimitedListToStringArray(java.lang.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.

commaDelimitedListToSet

public static java.util.Set commaDelimitedListToSet(java.lang.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

arrayToDelimitedString

public static java.lang.String arrayToDelimitedString(java.lang.Object[] arr,
                                                      java.lang.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 ,)

collectionToDelimitedString

public static java.lang.String collectionToDelimitedString(java.util.Collection c,
                                                           java.lang.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 ",")

iteratorToDelimitedString

public static java.lang.String iteratorToDelimitedString(java.util.Iterator itr,
                                                         java.lang.String delim)
Convenience method to return a Collection as a delimited (e.g. CSV) String. Useful for toString() implementations
Parameters:
itr - Iterator of the collection to display
delim - delimiter to use (probably a ,)


Rod Johnson and Spring contributors 2001-2003.