org.springframework.util
Class StringUtils

java.lang.Object
  extended byorg.springframework.util.StringUtils

public abstract class StringUtils
extends java.lang.Object

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.

Since:
16 April 2001
Version:
$Id: StringUtils.java,v 1.11 2004/03/19 16:45:29 jhoeller Exp $
Author:
Rod Johnson, Juergen Hoeller, Keith Donald
See Also:
org.apache.commons.lang.StringUtils

Constructor Summary
StringUtils()
           
 
Method Summary
static java.lang.String[] addStringToArray(java.lang.String[] arr, java.lang.String s)
          Append the given String to the given String array, returning a new array consisting of the input array contents plus the given String.
static java.lang.String arrayToCommaDelimitedString(java.lang.Object[] arr)
          Convenience method to return a String array as a CSV String.
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 collectionToCommaDelimitedString(java.util.Collection c)
          Convenience method to return a Collection as a CSV String.
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 delim)
          Take a String which is a delimited list and convert it to a String array.
static boolean hasLength(java.lang.String str)
          Checks if a String has length.
static boolean hasText(java.lang.String str)
          Checks if a String has text.
static java.lang.String replace(java.lang.String inString, java.lang.String oldPattern, java.lang.String newPattern)
          Replace all occurences of a substring within a string with another string.
static java.lang.String[] tokenizeToStringArray(java.lang.String s, java.lang.String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
          Tokenize the given String into a String array via a StringTokenizer.
static java.lang.String uncapitalize(java.lang.String str)
          Uncapitalizes a String, changing the first letter to lower case as per Character.toLowerCase(char).
static java.lang.String unqualify(java.lang.String qualifiedName)
          Unqualifies a string qualified by a '.' dot character.
static java.lang.String unqualify(java.lang.String qualifiedName, char separator)
          Unqualifies a string qualified by a separator character.
 
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. Return 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)
Replace 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 - the 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.

tokenizeToStringArray

public static java.lang.String[] tokenizeToStringArray(java.lang.String s,
                                                       java.lang.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, String.trim()

delimitedListToStringArray

public static java.lang.String[] delimitedListToStringArray(java.lang.String s,
                                                            java.lang.String delim)
Take a String which is a delimited list and convert it to a String array.

Parameters:
s - String
delim - delim (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, or 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. E.g. 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. E.g. useful for toString() implementations.

Parameters:
c - Collection to display
delim - delimiter to use (probably a ",")

arrayToCommaDelimitedString

public static java.lang.String arrayToCommaDelimitedString(java.lang.Object[] arr)
Convenience method to return a String array as a CSV String. E.g. useful for toString() implementations.

Parameters:
arr - array to display. Elements may be of any type (toString will be called on each element).

collectionToCommaDelimitedString

public static java.lang.String collectionToCommaDelimitedString(java.util.Collection c)
Convenience method to return a Collection as a CSV String. E.g. useful for toString() implementations.

Parameters:
c - Collection to display

addStringToArray

public static java.lang.String[] addStringToArray(java.lang.String[] arr,
                                                  java.lang.String s)
Append the given String to the given String array, returning a new array consisting of the input array contents plus the given String.

Parameters:
arr - the array to append to
s - the String to append
Returns:
the new array

hasLength

public static boolean hasLength(java.lang.String str)
Checks if a String has length.

 StringUtils.hasLength(null) = false
 StringUtils.hasLength("") = false
 StringUtils.hasLength(" ") = true
 StringUtils.hasLength("Hello") = true
 

Parameters:
str - the String to check, may be null
Returns:
true if the String is has length and is not null

hasText

public static boolean hasText(java.lang.String str)
Checks if a String has text. More specifically, returns true if the string not null, it's length is > 0, and it has at least one non-whitespace character.

 StringUtils.hasText(null) = false
 StringUtils.hasText("") = false
 StringUtils.hasText(" ") = false
 StringUtils.hasText("12345") = true
 StringUtils.hasText(" 12345 ") = true
 

Parameters:
str - the String to check, may be null
Returns:
true if the String is not null, length > 0, and not whitespace only

unqualify

public static java.lang.String unqualify(java.lang.String qualifiedName)
Unqualifies a string qualified by a '.' dot character. For example, "this.name.is.qualified", returns "qualified".

Parameters:
qualifiedName - the qualified name

unqualify

public static java.lang.String unqualify(java.lang.String qualifiedName,
                                         char separator)
Unqualifies a string qualified by a separator character. For example, "this:name:is:qualified" returns "qualified" if using a ':' separator.

Parameters:
qualifiedName - the qualified name
separator - the separator

uncapitalize

public static java.lang.String uncapitalize(java.lang.String str)
Uncapitalizes a String, changing the first letter to lower case as per Character.toLowerCase(char). No other letters are changed.

Parameters:
str - the String to uncapitalize, may be null
Returns:
the uncapitalized String, null if null String input


Copyright (C) 2003-2004 The Spring Framework Project.