org.teiid.core.util
Class StringUtil

java.lang.Object
  extended by org.teiid.core.util.StringUtil

public final class StringUtil
extends java.lang.Object

This is a common place to put String utility methods.


Nested Class Summary
static interface StringUtil.Constants
           
 
Field Summary
static java.util.Comparator CASE_INSENSITIVE_ORDER
           
static java.util.Comparator CASE_SENSITIVE_ORDER
           
static java.lang.String LINE_SEPARATOR
          The String that should be used to separate lines; defaults to #NEW_LINE
static java.lang.String LINE_SEPARATOR_PROPERTY_NAME
          The name of the System property that specifies the string that should be used to separate lines.
static java.lang.String SINGLE_QUOTE
          The String "'"
 
Method Summary
static java.lang.String collapseWhitespace(java.lang.String raw)
          Removes extraneous whitespace from a string.
static java.lang.String computeDisplayableForm(java.lang.String str)
          Compute a displayable form of the specified string.
static java.lang.String computeDisplayableForm(java.lang.String str, java.lang.String defaultValue)
          Compute a displayable form of the specified string.
static java.lang.String computeDisplayableFormOfConstant(java.lang.String text)
           
static java.lang.String computeDisplayableFormOfConstant(java.lang.String text, java.lang.String defaultValue)
           
static java.lang.String computePluralForm(java.lang.String str)
           
static java.lang.String computePluralForm(java.lang.String str, java.lang.String defaultValue)
           
static java.lang.String createFileName(java.lang.String str)
          Create a valid filename from the given String.
static java.util.regex.Pattern createPattern(java.lang.String pattern, boolean isCaseSensitive)
          Creates a regular expression pattern from the pattern string (which is our old 'StringMatcher' format).
static java.lang.String enclosedInSingleQuotes(java.lang.String aString)
          Utility to return a string enclosed in ''.
static boolean endsWithIgnoreCase(java.lang.String text, java.lang.String suffix)
          Tests if the string ends with the specified suffix.
static java.lang.String firstLetterUppercase(java.lang.String str)
          Make the first letter uppercase
static java.lang.String getFirstToken(java.lang.String str, java.lang.String delimiter)
          Return the first token in the string.
static java.lang.String getLastToken(java.lang.String str, java.lang.String delimiter)
          Return the last token in the string.
static java.lang.String[] getLines(java.lang.String value)
           
static java.lang.String getLineSeparator()
           
static java.lang.String getStackTrace(java.lang.Throwable t)
           
static int getTokenCount(java.lang.String str, java.lang.String delimiter)
          Return the number of tokens in a string that are seperated by the delimiter.
static java.util.List getTokens(java.lang.String str, java.lang.String delimiter)
          Return the tokens in a string in a list.
static int indexOfIgnoreCase(java.lang.String text, java.lang.String str)
          Returns the index within this string of the first occurrence of the specified substring.
static boolean isBoolean(java.lang.String text)
          Returns whether the specified text represents a boolean value, i.e., whether it equals "true" or "false" (case-insensitive).
static boolean isDigit(char c)
           
static boolean isDigits(java.lang.String str)
          Determine if the string passed in has all digits as its contents
static boolean isEmpty(java.lang.String text)
           Returns whether the specified text is either empty or null.
static boolean isLetter(char c)
           
static boolean isLetterOrDigit(char c)
           
static boolean isValid(java.lang.String str)
           
static java.lang.String join(java.util.List strings, java.lang.String delimiter)
          Join string pieces and separate with a delimiter.
static int occurrences(java.lang.String input, java.lang.String token)
          Return the number of occurrences of token string that occurs in input string.
static java.lang.String replace(java.lang.String source, java.lang.String search, java.lang.String replace)
           
static void replaceAll(java.lang.StringBuffer source, java.lang.String search, java.lang.String replace)
           
static java.lang.String replaceAll(java.lang.String source, java.lang.String search, java.lang.String replace)
           
static java.util.List split(java.lang.String str, java.lang.String splitter)
          Split a string into pieces based on delimiters.
static java.util.List splitOnEntireString(java.lang.String target, java.lang.String delimiter)
          Break a string into pieces based on matching the full delimiter string in the text.
static java.util.List splitPreservingQuotedSubstring(java.lang.String str, java.lang.String splitter)
          Split a string into pieces based on delimiters preserving spaces in quoted substring as on element in the returned list.
static boolean startsWithIgnoreCase(java.lang.String text, java.lang.String prefix)
          Tests if the string starts with the specified prefix.
static java.lang.String toFixedLength(java.lang.String input, int desiredLength)
          If input == null OR input.length() < desiredLength, pad to desiredLength with spaces.
static java.lang.String toLowerCase(java.lang.String str)
           
static java.lang.String toString(java.lang.Object[] array)
          Return a stringified version of the array, using a ',' as a delimiter
static java.lang.String toString(java.lang.Object[] array, java.lang.String delim)
          Return a stringified version of the array.
static java.lang.String toUpperCase(java.lang.String str)
           
static java.lang.String truncString(java.lang.String in, int len)
          Simple static method to tuncate Strings to given length.
static
<T> T
valueOf(java.lang.String value, java.lang.Class type)
          Convert the given value to specified type.
static java.lang.String wrap(java.lang.String str, int maxCharPerLine)
          Simple utility method to wrap a string by inserting line separators creating multiple lines each with length no greater than the user specified maximum.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SINGLE_QUOTE

public static final java.lang.String SINGLE_QUOTE
The String "'"

See Also:
Constant Field Values

LINE_SEPARATOR_PROPERTY_NAME

public static final java.lang.String LINE_SEPARATOR_PROPERTY_NAME
The name of the System property that specifies the string that should be used to separate lines. This property is a standard environment property that is usually set automatically.

See Also:
Constant Field Values

LINE_SEPARATOR

public static final java.lang.String LINE_SEPARATOR
The String that should be used to separate lines; defaults to #NEW_LINE


CASE_INSENSITIVE_ORDER

public static final java.util.Comparator CASE_INSENSITIVE_ORDER

CASE_SENSITIVE_ORDER

public static final java.util.Comparator CASE_SENSITIVE_ORDER
Method Detail

getLineSeparator

public static java.lang.String getLineSeparator()

enclosedInSingleQuotes

public static java.lang.String enclosedInSingleQuotes(java.lang.String aString)
Utility to return a string enclosed in ''. Creation date: (12/2/99 12:05:10 PM)


join

public static java.lang.String join(java.util.List strings,
                                    java.lang.String delimiter)
Join string pieces and separate with a delimiter. Similar to the perl function of the same name. If strings or delimiter are null, null is returned. Otherwise, at least an empty string will be returned.

Parameters:
strings - String pieces to join
delimiter - Delimiter to put between string pieces
Returns:
One merged string
See Also:
split(java.lang.String, java.lang.String)

toString

public static java.lang.String toString(java.lang.Object[] array,
                                        java.lang.String delim)
Return a stringified version of the array.

Parameters:
array - the array
delim - the delimiter to use between array components
Returns:
the string form of the array

toString

public static java.lang.String toString(java.lang.Object[] array)
Return a stringified version of the array, using a ',' as a delimiter

Parameters:
array - the array
Returns:
the string form of the array
See Also:
toString(Object[], String)

split

public static java.util.List split(java.lang.String str,
                                   java.lang.String splitter)
Split a string into pieces based on delimiters. Similar to the perl function of the same name. The delimiters are not included in the returned strings.

Parameters:
str - Full string
splitter - Characters to split on
Returns:
List of String pieces from full string
See Also:
join(java.util.List, java.lang.String)

splitOnEntireString

public static java.util.List splitOnEntireString(java.lang.String target,
                                                 java.lang.String delimiter)
Break a string into pieces based on matching the full delimiter string in the text. The delimiter is not included in the returned strings.

Parameters:
target - The text to break up.
delimiter - The sub-string which is used to break the target.
Returns:
List of String from the target.

splitPreservingQuotedSubstring

public static java.util.List splitPreservingQuotedSubstring(java.lang.String str,
                                                            java.lang.String splitter)
Split a string into pieces based on delimiters preserving spaces in quoted substring as on element in the returned list. The delimiters are not included in the returned strings.

Parameters:
str - Full string
splitter - Characters to split on
Returns:
List of String pieces from full string
See Also:
join(java.util.List, java.lang.String)

replace

public static java.lang.String replace(java.lang.String source,
                                       java.lang.String search,
                                       java.lang.String replace)

replaceAll

public static java.lang.String replaceAll(java.lang.String source,
                                          java.lang.String search,
                                          java.lang.String replace)

replaceAll

public static void replaceAll(java.lang.StringBuffer source,
                              java.lang.String search,
                              java.lang.String replace)

truncString

public static java.lang.String truncString(java.lang.String in,
                                           int len)
Simple static method to tuncate Strings to given length.

Parameters:
in - the string that may need tuncating.
len - the lenght that the string should be truncated to.
Returns:
a new String containing chars with length <= len or null if input String is null.

wrap

public static java.lang.String wrap(java.lang.String str,
                                    int maxCharPerLine)
Simple utility method to wrap a string by inserting line separators creating multiple lines each with length no greater than the user specified maximum. The method parses the given string into tokens using a space delimiter then reassembling the tokens into the resulting string while inserting separators when required. If the number of characters in a single token is greater than the specified maximum, the token will not be split but instead the maximum will be exceeded.

Parameters:
str - the string that may need tuncating.
maxCharPerLine - the max number of characters per line
Returns:
a new String containing line separators or the original string if its length was less than the maximum.

getTokens

public static java.util.List getTokens(java.lang.String str,
                                       java.lang.String delimiter)
Return the tokens in a string in a list. This is particularly helpful if the tokens need to be processed in reverse order. In that case, a list iterator can be acquired from the list for reverse order traversal.

Parameters:
str - String to be tokenized
delimiter - Characters which are delimit tokens
Returns:
List of string tokens contained in the tokenized string

getTokenCount

public static int getTokenCount(java.lang.String str,
                                java.lang.String delimiter)
Return the number of tokens in a string that are seperated by the delimiter.

Parameters:
str - String to be tokenized
delimiter - Characters which are delimit tokens
Returns:
Number of tokens seperated by the delimiter

occurrences

public static int occurrences(java.lang.String input,
                              java.lang.String token)
Return the number of occurrences of token string that occurs in input string. Note: token is case sensitive.

Parameters:
input -
token -
Returns:
int

getLastToken

public static java.lang.String getLastToken(java.lang.String str,
                                            java.lang.String delimiter)
Return the last token in the string.

Parameters:
str - String to be tokenized
delimiter - Characters which are delimit tokens
Returns:
the last token contained in the tokenized string

getFirstToken

public static java.lang.String getFirstToken(java.lang.String str,
                                             java.lang.String delimiter)
Return the first token in the string.

Parameters:
str - String to be tokenized
delimiter - Characters which are delimit tokens
Returns:
the first token contained in the tokenized string

computeDisplayableForm

public static java.lang.String computeDisplayableForm(java.lang.String str)
Compute a displayable form of the specified string. This algorithm attempts to create a string that contains words that begin with uppercase characters and that are separated by a single space. For example, the following are the outputs of some sample inputs:
  • "aName" is converted to "A Name"
  • "Name" is converted to "Name"
  • "NAME" is converted to "NAME"
  • "theName" is converted to "The Name"
  • "theBIGName" is converted to "The BIG Name"
  • "the BIG Name" is converted to "The BIG Name"
  • "the big Name" is converted to "The Big Name"
  • "theBIG" is converted to "The BIG"
  • "SQLIndex" is converted to "SQL Index"
  • "SQLIndexT" is converted to "SQL Index T"
  • "SQLIndex T" is converted to "SQL Index T"
  • "SQLIndex t" is converted to "SQL Index T"
  • Parameters:
    str - String to be converted; may be null
    Returns:
    the displayable form of str, or an empty string if str is either null or zero-length; never null

    computeDisplayableForm

    public static java.lang.String computeDisplayableForm(java.lang.String str,
                                                          java.lang.String defaultValue)
    Compute a displayable form of the specified string. This algorithm attempts to create a string that contains words that begin with uppercase characters and that are separated by a single space. For example, the following are the outputs of some sample inputs:
  • "aName" is converted to "A Name"
  • "Name" is converted to "Name"
  • "NAME" is converted to "NAME"
  • "theName" is converted to "The Name"
  • "theBIGName" is converted to "The BIG Name"
  • "the BIG Name" is converted to "The BIG Name"
  • "the big Name" is converted to "The Big Name"
  • "theBIG" is converted to "The BIG"
  • "SQLIndex" is converted to "SQL Index"
  • "SQLIndexT" is converted to "SQL Index T"
  • "SQLIndex T" is converted to "SQL Index T"
  • "SQLIndex t" is converted to "SQL Index T"
  • An exception is "MetaMatrix", which is always treated as a single word

    Parameters:
    str - String to be converted; may be null
    defaultValue - the default result if the input is either null or zero-length.
    Returns:
    the displayable form of str, or the default value if str is either null or zero-length.

    computeDisplayableFormOfConstant

    public static java.lang.String computeDisplayableFormOfConstant(java.lang.String text)
    Since:
    3.0

    computeDisplayableFormOfConstant

    public static java.lang.String computeDisplayableFormOfConstant(java.lang.String text,
                                                                    java.lang.String defaultValue)
    Since:
    3.0

    computePluralForm

    public static java.lang.String computePluralForm(java.lang.String str)

    computePluralForm

    public static java.lang.String computePluralForm(java.lang.String str,
                                                     java.lang.String defaultValue)

    getStackTrace

    public static java.lang.String getStackTrace(java.lang.Throwable t)

    isBoolean

    public static boolean isBoolean(java.lang.String text)
    Returns whether the specified text represents a boolean value, i.e., whether it equals "true" or "false" (case-insensitive).

    Since:
    4.0

    isEmpty

    public static boolean isEmpty(java.lang.String text)

    Returns whether the specified text is either empty or null.

    Parameters:
    text - The text to check; may be null;
    Returns:
    True if the specified text is either empty or null.
    Since:
    4.0

    indexOfIgnoreCase

    public static int indexOfIgnoreCase(java.lang.String text,
                                        java.lang.String str)
    Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:
     this.startsWith(str, k)
     
    is true.

    Parameters:
    text - any string.
    str - any string.
    Returns:
    if the str argument occurs as a substring within text, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned. If the text or str argument is null or empty then -1 is returned.

    startsWithIgnoreCase

    public static boolean startsWithIgnoreCase(java.lang.String text,
                                               java.lang.String prefix)
    Tests if the string starts with the specified prefix.

    Parameters:
    text - the string to test.
    prefix - the prefix.
    Returns:
    true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the prefix is an empty string or is equal to the text String object as determined by the Object.equals(Object) method. If the text or prefix argument is null false is returned.
    Since:
    JDK1. 0

    endsWithIgnoreCase

    public static boolean endsWithIgnoreCase(java.lang.String text,
                                             java.lang.String suffix)
    Tests if the string ends with the specified suffix.

    Parameters:
    text - the string to test.
    suffix - the suffix.
    Returns:
    true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise. Note that the result will be true if the suffix is the empty string or is equal to this String object as determined by the Object.equals(Object) method. If the text or suffix argument is null false is returned.

    isDigits

    public static boolean isDigits(java.lang.String str)
    Determine if the string passed in has all digits as its contents

    Parameters:
    str -
    Returns:
    true if digits; false otherwise

    createPattern

    public static java.util.regex.Pattern createPattern(java.lang.String pattern,
                                                        boolean isCaseSensitive)
    Creates a regular expression pattern from the pattern string (which is our old 'StringMatcher' format). Copied from Eclipse's PatternConstructor class.

    Parameters:
    pattern - The search pattern
    isCaseSensitive - Set to true to create a case insensitve pattern
    Returns:
    The created pattern

    collapseWhitespace

    public static java.lang.String collapseWhitespace(java.lang.String raw)
    Removes extraneous whitespace from a string. By it's nature, it will be trimmed also.

    Parameters:
    raw -
    Returns:
    Since:
    5.0

    toFixedLength

    public static java.lang.String toFixedLength(java.lang.String input,
                                                 int desiredLength)
    If input == null OR input.length() < desiredLength, pad to desiredLength with spaces. If input.length() > desiredLength, chop at desiredLength.

    Parameters:
    input - Input text
    desiredLength - Desired length
    Returns:
    Since:
    5.0

    isLetter

    public static boolean isLetter(char c)

    isDigit

    public static boolean isDigit(char c)

    isLetterOrDigit

    public static boolean isLetterOrDigit(char c)

    isValid

    public static boolean isValid(java.lang.String str)

    toUpperCase

    public static java.lang.String toUpperCase(java.lang.String str)

    toLowerCase

    public static java.lang.String toLowerCase(java.lang.String str)

    createFileName

    public static java.lang.String createFileName(java.lang.String str)
    Create a valid filename from the given String.

    Parameters:
    str - The String to convert to a valid filename.
    defaultName - The default name to use if only special characters exist.
    Returns:
    String A valid filename.

    firstLetterUppercase

    public static java.lang.String firstLetterUppercase(java.lang.String str)
    Make the first letter uppercase

    Parameters:
    str -
    Returns:
    The string with the first letter being changed to uppercase
    Since:
    5.5

    valueOf

    public static <T> T valueOf(java.lang.String value,
                                java.lang.Class type)
    Convert the given value to specified type.

    Parameters:
    value -
    type -
    Returns:

    getLines

    public static java.lang.String[] getLines(java.lang.String value)


    Copyright © 2010. All Rights Reserved.