com.tc.util
Class StringUtil

java.lang.Object
  extended by com.tc.util.StringUtil

public class StringUtil
extends java.lang.Object

String utility methods.


Field Summary
static java.lang.String EMPTY
          The empty string
static java.lang.String LINE_SEPARATOR
           
static java.lang.String NULL_STRING
          A string representing a null value: ""
static char SPACE
          A space character
static java.lang.String SPACE_STRING
          A space string
 
Constructor Summary
StringUtil()
           
 
Method Summary
static java.lang.String getNonNull(java.lang.String s)
          Get a non-null version of the String.
static java.lang.String getNonNull(java.lang.String s, java.lang.String nullToken)
          For a string s, if non-null return s, else return nullToken.
static java.lang.String indentLines(java.lang.String source)
          Indent lines using a single tab by inserting them into source after line breaks and returning a new String.
static java.lang.StringBuffer indentLines(java.lang.StringBuffer source, int indentLevel)
          Indent lines using tabs by inserting them into source and returning source.
static java.lang.StringBuffer indentLines(java.lang.StringBuffer source, int indentLevel, char indentChar)
          Indent lines in the StringBuffer (line breaks found at \n) with indentChar repeated indentLevel times.
static java.lang.String indentLines(java.lang.String source, int indentLevel)
          Indent lines using tabs by inserting them into source after line breaks and returning a new String.
static int indexOfStringBuffer(java.lang.StringBuffer source, java.lang.String search, int start)
          Find index of "search" in "source", starting at "start" index.
static java.lang.String reduce(java.lang.String source)
          Reduces the size that a string occupies to the minimal possible by ensuring that the back-end char array contains exactly the characters that are needed, and no more.
static java.lang.String replaceAll(java.lang.String source, java.lang.String search, java.lang.String replace, boolean skipQuotedStrings)
          Simple search/replace for non-pattern strings.
static java.lang.String safeToString(java.lang.Object object)
          Normal toString(), but convert null to the NULL_STRING.
static java.lang.String toPaddedString(long value, int radix, int paddedWidth)
          Format value to string using radix, then prepend with 0's out to paddedWidth.
static java.lang.String toString(java.lang.Object[] objs)
          Helper method to convert object array [a, b, c] to comma-separated string "a, b, c".
static java.lang.String toString(java.lang.Object[] objs, java.lang.String separator, java.lang.String prefix, java.lang.String postfix)
          Creates a String representation of an array of objects by calling toString on each one.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SPACE

public static final char SPACE
A space character

See Also:
Constant Field Values

SPACE_STRING

public static final java.lang.String SPACE_STRING
A space string

See Also:
Constant Field Values

EMPTY

public static final java.lang.String EMPTY
The empty string

See Also:
Constant Field Values

NULL_STRING

public static final java.lang.String NULL_STRING
A string representing a null value: ""

See Also:
Constant Field Values

LINE_SEPARATOR

public static final java.lang.String LINE_SEPARATOR
Constructor Detail

StringUtil

public StringUtil()
Method Detail

safeToString

public static final java.lang.String safeToString(java.lang.Object object)
Normal toString(), but convert null to the NULL_STRING.

Returns:
Always a string, never null

indentLines

public static java.lang.String indentLines(java.lang.String source)
Indent lines using a single tab by inserting them into source after line breaks and returning a new String.

Parameters:
source - Source string, may NOT be null
Returns:
New string, updated with indents
Throws:
java.lang.NullPointerException - If source is null

indentLines

public static java.lang.String indentLines(java.lang.String source,
                                           int indentLevel)
Indent lines using tabs by inserting them into source after line breaks and returning a new String.

Parameters:
source - Source string, may NOT be null
indentLevel - Number of tabs to insert, must be >= 0
Returns:
Original buffer, updated
Throws:
java.lang.IllegalArgumentException - If indentLevel < 0
java.lang.NullPointerException - If source is null

indentLines

public static java.lang.StringBuffer indentLines(java.lang.StringBuffer source,
                                                 int indentLevel)
Indent lines using tabs by inserting them into source and returning source.

Parameters:
source - Source buffer, may be null
indentLevel - Number of tabs to insert, must be >= 0
Returns:
Original buffer, updated
Throws:
java.lang.IllegalArgumentException - If indentLevel < 0

indentLines

public static java.lang.StringBuffer indentLines(java.lang.StringBuffer source,
                                                 int indentLevel,
                                                 char indentChar)
Indent lines in the StringBuffer (line breaks found at \n) with indentChar repeated indentLevel times.

Parameters:
source - Source buffer, may be null
indentLevel - Number of chars to indent, must be >= 0
indentChar - Indent character (usually ' ' or '\t')
Returns:
Original buffer, updated
Throws:
java.lang.IllegalArgumentException - If indentLevel < 0

indexOfStringBuffer

public static int indexOfStringBuffer(java.lang.StringBuffer source,
                                      java.lang.String search,
                                      int start)
Find index of "search" in "source", starting at "start" index.

Parameters:
source - Source buffer, must be non-null
search - Search string, must be non-null
start - Start index, should be 0<=startReturns:
Index of found string or -1 if not found
Throws:
java.lang.NullPointerException - If source or search is null

toString

public static final java.lang.String toString(java.lang.Object[] objs,
                                              java.lang.String separator,
                                              java.lang.String prefix,
                                              java.lang.String postfix)
Creates a String representation of an array of objects by calling toString on each one. Formatting is controlled by the parameters.

Parameters:
objs - (required) the array of objects to display
separator - (optional) a string to place between each object
prefix - (optional) a string to prefix each object with
postfix - (optional) a string to append to each object
Returns:
a String representation of the array, never returns null

toString

public static final java.lang.String toString(java.lang.Object[] objs)
Helper method to convert object array [a, b, c] to comma-separated string "a, b, c".

Parameters:
objs - Array of objects, can be null
Returns:
String, never null

toPaddedString

public static final java.lang.String toPaddedString(long value,
                                                    int radix,
                                                    int paddedWidth)
Format value to string using radix, then prepend with 0's out to paddedWidth. If the formatted value is > paddedWidth, then the value is returned.

Parameters:
value - Long value, must be >= 0
radix - The radix to use when representing the value
paddedWidth - The width to pad to by prepending 0
Returns:
Padded formatted string value for the long value, never null

replaceAll

public static final java.lang.String replaceAll(java.lang.String source,
                                                java.lang.String search,
                                                java.lang.String replace,
                                                boolean skipQuotedStrings)
Simple search/replace for non-pattern strings. optionally skipping over quoted strings.

Parameters:
source - the original string to perform the search/replace on, a modified version of this is returned, if null then null will be returned immediately
search - the string to search for in source, if null then null is returned immediately
replace - the string to replace search occurrences in source, if null then the search string is simply removed from source and not replaced with anything
skipQuotedStrings - if true then quoted strings will be skipped over
Returns:
a modified version of source, or null if source or search are null

reduce

public static final java.lang.String reduce(java.lang.String source)
Reduces the size that a string occupies to the minimal possible by ensuring that the back-end char array contains exactly the characters that are needed, and no more. Note that this method doesn't modify the original string as they are immutable, a new string is returned instead.

Parameters:
source - the string that needs to be reduced
Returns:
the reduced string, null if source is null

getNonNull

public static final java.lang.String getNonNull(java.lang.String s,
                                                java.lang.String nullToken)
For a string s, if non-null return s, else return nullToken.

Parameters:
s - The starting string
nullToken - The null token
Returns:
s or nullToken depending on s

getNonNull

public static final java.lang.String getNonNull(java.lang.String s)
Get a non-null version of the String.

Parameters:
s - The string
Returns:
Either s or the empty string if s was null


Copyright © 2010 Terracotta, Inc.. All Rights Reserved.