02/28/2005
 

com.vignette.portal.util
Class StringUtils

java.lang.Object
  |
  +--com.vignette.portal.util.StringUtils

public final class StringUtils
extends Object

Various static utility methods for manipulating strings. This class need not be instantiated.


Method Summary
static boolean contains(String string1, String string2)
          Determine whether or not the first string contains the second string.
static String convertEncoding(String oldValue, String oldCharacterEncoding, String newCharacterEncoding)
          Converts a string produced from bytes in one encoding into the string that would have been constructed if the new encoding was used.
static int indexOfIgnoreCase(String string1, String string2)
          Determine if the first string contains the second string.
static int indexOfIgnoreCase(String string1, String string2, int fromIndex)
          Determine if the first string contains the second string after fromIndex.
static boolean isEmpty(String string)
          Determine if the passed in string is empty or null, where not empty is defined as having at least one character.
static boolean isEmptyIgnoreWhitespace(String string)
          Determine if the passed in string is empty or null, where not empty is defined as having at least one non-whitespace character.
static boolean parseBoolean(String string, boolean defaultValue)
          Inspect the given string for "true" or "false", ignoring case.
static byte parseByte(String string, byte defaultValue)
          Assuming the specified String represents a byte, returns that byte's value.
static double parseDouble(String string, double defaultValue)
          Assuming the specified String represents a double, returns that double's value.
static float parseFloat(String string, float defaultValue)
          Assuming the specified String represents a float, returns that float's value.
static int parseInt(String string, int defaultValue)
          Assuming the specified String represents a int, returns that int's value.
static long parseLong(String string, long defaultValue)
          Assuming the specified String represents a long, returns that long's value.
static short parseShort(String string, short defaultValue)
          Assuming the specified String represents a short, returns that short's value.
static String removeWhitespace(String input)
          Removes whitespace from a string.
static String repeat(int count, char character)
          Returns a string of the given length (count) consisting entirely of character characters.
static String replace(String input, String find, String replace)
          Replace all occurrences in 'input' of 'find' with 'replace'.
static String[] split(String string, String delimiter)
          Splits the given string into an array of substrings at the given set of character delimiters.
static String toString(Object object)
          Returns the string value of the given object or an empty string if the object is null.
static String toString(Object object, String defaultValue, boolean ignoreWhitespace)
          Returns the string value of the given object.
static String toString(String initialValue, String defaultValue, boolean ignoreWhitespace)
          Returns the given initial value or the default value specified in the second parameter should the initial value be empty.
static String trim(Object object)
          Trim the string value of the object passed in.
static String truncate(String string, int length, boolean absolute)
          Returns the first length characters (or less, if absolute is false) from the input string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toString

public static String toString(Object object)
Returns the string value of the given object or an empty string if the object is null.
Parameters:
object - The object for which to retrieve the String value.
Returns:
The string value of the given object or an empty string if the object is null.

toString

public static String toString(Object object,
                              String defaultValue,
                              boolean ignoreWhitespace)
Returns the string value of the given object. If the object is null or an empty string, returns the default value as specified in the second parameter.
Parameters:
object - The object for which to retrieve the String value.
defaultValue - The default value to be returned.
ignoreWhitespace - True if whitespace should be ignored when determining if the Object's String value is empty.
Returns:
The string value of the given object or the alternate default_value should the string be empty or null.

toString

public static String toString(String initialValue,
                              String defaultValue,
                              boolean ignoreWhitespace)
Returns the given initial value or the default value specified in the second parameter should the initial value be empty.
Parameters:
initialValue - the intended return string
defaultValue - the default return string
ignoreWhitespace - True if whitespace does not count towards the initialValue not being empty.
Returns:
initialValue if it is non-null and (depending on ignoreWhitespace) contains at least one non-whitespace character, otherwise returns defaultValue.

trim

public static String trim(Object object)
Trim the string value of the object passed in. All whitespace is trimed from both ends of the String. Null is handled by returning null.
Parameters:
object - The object for which the trimmed string value is to be returned.
Returns:
The string value of the object passed in or null if the object is null.

isEmpty

public static boolean isEmpty(String string)
Determine if the passed in string is empty or null, where not empty is defined as having at least one character.
Parameters:
string - The string value to be tested.
Returns:
true if the passed in string is empty or null. Otherwise returns false.

isEmptyIgnoreWhitespace

public static boolean isEmptyIgnoreWhitespace(String string)
Determine if the passed in string is empty or null, where not empty is defined as having at least one non-whitespace character.
Parameters:
string - The string value to be tested.
Returns:
true if the passed in string is empty or null. Otherwise returns false.

contains

public static boolean contains(String string1,
                               String string2)
Determine whether or not the first string contains the second string. This test is not case sensitive. For a case sensitive result use String.indexOf(String string). If either parameter is null, this method will return false. If string2 is an empty string, this method will return true (i.e. every string contains an empty string).
Parameters:
string1 - The string to be tested as the container of the second string.
string2 - The string that is potentially contained inside the first string.
Returns:
true If the first string contains the second string.

parseBoolean

public static boolean parseBoolean(String string,
                                   boolean defaultValue)
Inspect the given string for "true" or "false", ignoring case. If the string is neither true nor false, return the value of the given default boolean.
Parameters:
string - The string to be parsed into a boolean value.
defaultValue - The default value to be returned in the event the supplied string cannot be parsed into a boolean.
Returns:
The boolean value of the given string, or the supplied default boolean value if the string cannot be parsed into a boolean.

parseByte

public static byte parseByte(String string,
                             byte defaultValue)
Assuming the specified String represents a byte, returns that byte's value. The radix is assumed to be 10.
Parameters:
string - The string to be parsed into a byte value.
defaultValue - The default value to be returned in the event the supplied string cannot be parsed into a byte.
Returns:
The byte value of the given string, or the supplied default byte value if the string cannot be parsed into a byte.

parseShort

public static short parseShort(String string,
                               short defaultValue)
Assuming the specified String represents a short, returns that short's value.
Parameters:
string - The string to be parsed into a short value.
defaultValue - The default value to be returned in the event the supplied string cannot be parsed into a short.
Returns:
The short value of the given string, or the supplied default short value if the string cannot be parsed into a short.

parseInt

public static int parseInt(String string,
                           int defaultValue)
Assuming the specified String represents a int, returns that int's value.
Parameters:
string - The string to be parsed into an int value.
defaultValue - The default value to be returned in the event the supplied string cannot be parsed into an int.
Returns:
The int value of the given string, or the supplied default int value if the string cannot be parsed into an int.

parseLong

public static long parseLong(String string,
                             long defaultValue)
Assuming the specified String represents a long, returns that long's value.
Parameters:
string - The string to be parsed into a long value.
defaultValue - The default value to be returned in the event the supplied string cannot be parsed into a long.
Returns:
The long value of the given string, or the supplied default long value if the string cannot be parsed into a long.

parseFloat

public static float parseFloat(String string,
                               float defaultValue)
Assuming the specified String represents a float, returns that float's value.
Parameters:
string - The string to be parsed into a float value.
defaultValue - The default value to be returned in the event the supplied string cannot be parsed into a float.
Returns:
The float value of the given string, or the supplied default float value if the string cannot be parsed into a float.

parseDouble

public static double parseDouble(String string,
                                 double defaultValue)
Assuming the specified String represents a double, returns that double's value.
Parameters:
string - The string to be parsed into a double value.
defaultValue - The default value to be returned in the event the supplied string cannot be parsed into a double.
Returns:
The double value of the given string, or the supplied default double value if the string cannot be parsed into a double.

indexOfIgnoreCase

public static int indexOfIgnoreCase(String string1,
                                    String string2)
Determine if the first string contains the second string. If so, return the index of the first occurance. The test is not case sensitive. If string2 is an empty string, 0 is returned.
Parameters:
string1 - The string to be tested as the container of the second string.
string2 - The string that may be contained in the first string.
Returns:
The index of the first occurance of the string2 within string1 or -1 if no occurances are found.

indexOfIgnoreCase

public static int indexOfIgnoreCase(String string1,
                                    String string2,
                                    int fromIndex)
Determine if the first string contains the second string after fromIndex. If so, return the index of the first occurance after fromIndex (i.e. Return the index of the leftmost occurance of string2 in string1 starting at fromIndex and searching to the right). The character at fromIndex is included in the search. The test is not case sensitive. If string2 is an empty string, fromIndex is returned.
Parameters:
string1 - The string to be tested as the container of the second string.
string2 - The string that may be contained in the first string.
fromIndex - The index from which to begin searching for the next occurance.
Returns:
The index of the first occurance of the string2 within string1 starting at fromIndex or -1 if no occurances are found.

split

public static String[] split(String string,
                             String delimiter)
Splits the given string into an array of substrings at the given set of character delimiters. For instance, split("a/b\\c/d\\baby/whee", "/\\") returns an array with elements "a", "b", "c", "d", "baby", "whee".
Parameters:
string - The string to be split.
delimiter - The delimiters to use to split the string in to smaller strings.
Returns:
An array of Strings, each one a token that was split using one of the supplied delimiters.

repeat

public static String repeat(int count,
                            char character)
Returns a string of the given length (count) consisting entirely of character characters.
Parameters:
count - The number of times to repeat the given character
character - The character to be repeated count times.
Returns:
A string of the given count length consisting entirely of character characters.

replace

public static String replace(String input,
                             String find,
                             String replace)
Replace all occurrences in 'input' of 'find' with 'replace'. The matching criteria is case sensitive.
Parameters:
input - The input string on which to perform the replacment operation.
find - The string to be replaced.
replace - The string to be used as a replacement for find.
Returns:
The result string of the replacement operation.

removeWhitespace

public static String removeWhitespace(String input)
Removes whitespace from a string. Whitespace is defined by the Character.isWhitespace(char) method of class java.lang.Character.
Parameters:
input - The input string on which to perform this operation.
Returns:
The result string of the removeWhitespace operation.

convertEncoding

public static String convertEncoding(String oldValue,
                                     String oldCharacterEncoding,
                                     String newCharacterEncoding)
                              throws UnsupportedEncodingException
Converts a string produced from bytes in one encoding into the string that would have been constructed if the new encoding was used.
Parameters:
oldValue - The original string value
oldCharacterEncoding - The original string encoding
newCharacterEncoding - The replacement string encoding
Returns:
The converted string
See Also:
String.getBytes(java.lang.String), String.String(byte[], java.lang.String)

truncate

public static String truncate(String string,
                              int length,
                              boolean absolute)
Returns the first length characters (or less, if absolute is false) from the input string.
Parameters:
string - the string to get characters from
length - the max number of characters to be returned
absolute - boolean flag; true to return exactly length characters from string or just string if it contains less than length characters, false to return as many whole words (non-whitespace character sequences separated by whitespace) from string as will fit within length characters
Returns:
the truncated string; empty if absolute is true and length is less than 0 or if absolute is false and string doesn't begin with any whole words that are less than length characters in length, never null

02/28/2005
 

Copyright and Trademark Notices