com.ibm.commons.util
Class StringUtil

java.lang.Object
  extended by com.ibm.commons.util.StringUtil

public class StringUtil
extends java.lang.Object

This class contains helpers for string manipulation.


Field Summary
static java.lang.String EMPTY_STRING
           
static java.lang.String[] EMPTY_STRING_ARRAY
           
static java.lang.String SPACE
           
 
Constructor Summary
StringUtil()
           
 
Method Summary
static int compareTo(java.lang.String s1, java.lang.String s2)
          Compares two strings.
static int compareToIgnoreCase(java.lang.String s1, java.lang.String s2)
          Compares two strings ignoring the case.
static java.lang.String concatStrings(java.lang.String[] strings, char sep, boolean trim)
          Concat a list of strings into a single string.
static boolean contains(java.lang.Object[] objects, java.lang.Object toTest)
           
static java.lang.String decrypt(java.lang.String pwd)
          Deprecated.  
static java.lang.String encrypt(java.lang.String pwd)
          Deprecated.  
static java.lang.String encrypt(java.lang.String pwd, java.lang.String ctx)
          Deprecated.  
static boolean endsWithIgnoreCase(java.lang.String s, java.lang.String suffix)
          Identical to String.endsWith but case insensitive.
static boolean equals(java.lang.Object str1, java.lang.Object str2)
          Test if the specified objects are equal.
static boolean equals(java.lang.String s1, java.lang.String s2)
          Test the string equality.
static boolean equalsIgnoreCase(java.lang.String s1, java.lang.String s2)
          Test the string equality, ignoring the case.
static java.lang.String expandProperties(java.lang.String s)
          Property expander This function replaces all the occurences of ${propname} by the actual property value
static java.lang.String format(java.lang.String fmt, java.lang.Object... parameters)
          Format a string.
static java.lang.String fromXmlInputString(java.lang.String s)
          Converts an HTML string to a Java one, converting the numeric entities (Ӓ) sent by the browser.
static java.lang.String generateCopyName(java.lang.String sourceName, java.lang.String[] listOfNames)
          Create a name with the _copy_x appended, where X is the next available number.
static java.lang.String getAutoGenNameFromJavaName(java.lang.String javaName)
           
static java.lang.String getDigest(java.lang.String str)
          Transforms a String into a fixed-length digest.
static java.lang.String[] getLineSeparators()
           
static char getMnemonicCharacter(java.lang.String text)
           
static java.lang.String getNextUniqueValue(java.lang.String name, long startingNum, java.lang.String[] listOfNames)
          Given the current name, append a long until a unique is found, starting at the given starting num.
static java.lang.String getNonNullString(java.lang.String string)
          Return a non-null string if this string is null
static java.lang.String getProperCaseString(java.lang.String sample)
          Returns a string whose first letter is a Capital letter, and a space is inserted anywhere an upper case letter exists in the existing string.
static char hexChar(int v)
           
static int hexValue(char c)
           
static int indexOfIgnoreCase(java.lang.String source, java.lang.String str)
          Returns the index within this string of the first occurrence of the specified string.
static int indexOfIgnoreCase(java.lang.String source, java.lang.String str, int fromIndex)
          Returns the index within this string of the first occurrence of the specified string.
static boolean isContainedWithin(java.lang.String s, java.lang.String[] container)
           
static boolean isEmpty(java.lang.String s)
          Test if a string is empty.
static boolean isEmpty(java.lang.String[] arr)
          Checks if an array of string is empty.
static boolean isEncrypted(java.lang.String pwd)
          Deprecated.  
static boolean isFalseValue(java.lang.String stringToTest)
           
static boolean isLineSeparator(java.lang.String s)
           
static boolean isNotEmpty(java.lang.String s)
          Test if a string is not empty.
static boolean isNotEmpty(java.lang.String[] arr)
          Checks if an array of string is not empty.
static boolean isSpace(java.lang.String s)
          Test if a string is only made of spaces.
static boolean isTrueValue(java.lang.String stringToTest)
           
static int lastIndexOfIgnoreCase(java.lang.String source, java.lang.String str)
          Returns the index within this string of the last occurrence of the specified string.
static int lastIndexOfIgnoreCase(java.lang.String source, java.lang.String str, int fromIndex)
          Returns the index within this string of the last occurrence of the specified string.
static java.lang.String ltrim(java.lang.String s)
          Trims the space characters from the beginning of a string.
static java.lang.String parseHtml(java.lang.String s)
          Strip extra spaces and html tags.
static java.lang.String removeLineBreaks(java.lang.String s)
          Remove line breaks.
static java.lang.String removeMnemonics(java.lang.String string)
          Remove the mnemonics from a string.
static java.lang.String removeMnemonics(java.lang.String string, boolean remove)
          A Helper function for conditionally calling removeMnemonics.
static java.lang.String repeat(char toRepeat, int count)
          Create a new string by repeating a character.
static java.lang.String repeat(java.lang.String toRepeat, int count)
          Create a new string by repeating a string.
static java.lang.String replace(java.lang.String string, char value, char replace)
          Replace all the occurrences of a character within a source string.
static java.lang.String replace(java.lang.String source, java.lang.String value, java.lang.String replace)
          Replace all the occurrences of a string within a source string.
static java.lang.String replaceFirst(java.lang.String source, java.lang.String value, java.lang.String replace)
          Replace the first occurrence of a string within a source string.
static java.lang.String rtrim(java.lang.String s)
          Trims the space characters from the end of a string.
static java.lang.String[] splitString(java.lang.String s, char sep)
          Breaks a string into an array of substrings, with the delimeter removed.
static java.lang.String[] splitString(java.lang.String s, char sep, boolean trim)
          Breaks a string into an array of substrings, with the delimeter removed.
static java.lang.String[] splitString(java.lang.String s, java.lang.String sep, boolean trim)
           
static boolean startsWithIgnoreCase(java.lang.String s, java.lang.String prefix)
          Identical to String.startsWith but case insensitive.
static boolean startsWithIgnoreCase(java.lang.String s, java.lang.String prefix, int start)
          Identical to String.startsWith but case insensitive.
static java.lang.String toHexValue(byte[] barr)
          Converts a byte array to a hexadecimal String equivalent.
static java.lang.String toString(boolean value)
          Convert a boolean to a string.
static java.lang.String toString(byte value)
          Convert a byte to a string.
static java.lang.String toString(char ch)
          Convert a character to a string.
static java.lang.String toString(double value)
          Convert a double to a string.
static java.lang.String toString(float value)
          Convert a float to a string.
static java.lang.String toString(int value)
          Convert an integer to a string.
static java.lang.String toString(int value, int digits, char fill)
          Convert a integer to its string representation and fill with characters on left to match the number of digits required.
static java.lang.String toString(long value)
          Convert a long to a string.
static java.lang.String toString(java.lang.Object o)
          Convert an object to a string.
static java.lang.String toString(java.lang.Object value, int max)
          Convert a object to its string representation.
static java.lang.String toString(short value)
          Convert a short to a string.
static java.lang.String[] toStringArray(java.util.Collection strings)
          Converts a collection full of Strings to a string array.
static java.lang.String toStringArray(java.lang.Object value, int used, int max)
           
static java.lang.String toUnsignedHex(int value, int nChars)
          Convert an integer to an Hexa string and pad the result with '0'
static java.lang.String trim(char[] array)
          Trims the spaces characters from both ends of of the character array.
static java.lang.String trim(java.lang.String s)
          Trims the space characters from both ends of a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_STRING_ARRAY

public static final java.lang.String[] EMPTY_STRING_ARRAY

SPACE

public static final java.lang.String SPACE
See Also:
Constant Field Values

EMPTY_STRING

public static final java.lang.String EMPTY_STRING
See Also:
Constant Field Values
Constructor Detail

StringUtil

public StringUtil()
Method Detail

equalsIgnoreCase

public static final boolean equalsIgnoreCase(java.lang.String s1,
                                             java.lang.String s2)
Test the string equality, ignoring the case. The 2 parameters can be null as far they are assimiled as empty.

Parameters:
s1 - the first string to compare
s2 - the first string to compare
Returns:
true if the 2 strings are equals, false otherwise

concatStrings

public static java.lang.String concatStrings(java.lang.String[] strings,
                                             char sep,
                                             boolean trim)
Concat a list of strings into a single string.

Parameters:
strings - the array of strings used
sep - the separator between strings
trim - indicate if the strings must be trimmed
Returns:
the concatened string

trim

public static final java.lang.String trim(java.lang.String s)
Trims the space characters from both ends of a string. All characters that have codes less than or equal to '\u0020' (the space character) are considered to be white space.

Parameters:
s - the string to edit
Returns:
the trimmed string

trim

public static final java.lang.String trim(char[] array)
Trims the spaces characters from both ends of of the character array.

Parameters:
array - the char array to trim
Returns:
the trimmed string

compareTo

public static int compareTo(java.lang.String s1,
                            java.lang.String s2)
Compares two strings. The comparison is based on the Unicode value of each character in the strings.

Parameters:
s1 - the first string
s2 - the second string
Returns:
the value 0 if s1 is equal to s2. A value less than 0 if s10 if s1>s2.

compareToIgnoreCase

public static int compareToIgnoreCase(java.lang.String s1,
                                      java.lang.String s2)
Compares two strings ignoring the case. The comparison is based on the Unicode value of each character in the strings. When sun will provide such a native String function, it will be more optimized.

Parameters:
s1 - the first string
s2 - the second string
Returns:
the value 0 if s1 is equal to s2. A value less than 0 if s10 if s1>s2.

equals

public static final boolean equals(java.lang.String s1,
                                   java.lang.String s2)
Test the string equality. The 2 parameters can be null as far they are assimiled as empty.

Parameters:
s1 - the first string to compare
s2 - the first string to compare
Returns:
true if the 2 strings are equals, false otherwise

equals

public static boolean equals(java.lang.Object str1,
                             java.lang.Object str2)
Test if the specified objects are equal. This method acts as Object.equals except that it checks for nullity.

Parameters:
str1 - the first object to test
str2 - the second object to test
Returns:
true if the 2 objects are equals, false otherwise

isEmpty

public static boolean isEmpty(java.lang.String s)
Test if a string is empty. A string is considered empty if it is either null or has a length of 0 characters.

Parameters:
s - the string to check
Returns:
true if the string is empty

isNotEmpty

public static boolean isNotEmpty(java.lang.String s)
Test if a string is not empty. A string is considered empty if it is either null or has a length of 0 characters.

Parameters:
s - the string to check
Returns:
true if the string is not empty

getNonNullString

public static java.lang.String getNonNullString(java.lang.String string)
Return a non-null string if this string is null


isSpace

public static final boolean isSpace(java.lang.String s)
Test if a string is only made of spaces. By spaces, we mean blank, tab, CR and LF.

Parameters:
s - the string to check
Returns:
true if the string is only made of spaces (or if it is empty)

format

public static final java.lang.String format(java.lang.String fmt,
                                            java.lang.Object... parameters)
Format a string. Format a string using a formt string and parameters. Each entry like {n} in the format string is replaced by the actual parameter value converted to a string.

Parameters:
fmt - the format string
p1 - the first message parameter
p2 - the second message parameter
p3 - the third message parameter
p4 - the fourth message parameter
p5 - the fifth message parameter
p6 - the sixth message parameter
Returns:
the formatted string

toString

public static java.lang.String toString(java.lang.Object o)
Convert an object to a string. The resulting string is limited to 32 characters.

Parameters:
o - the object to convert
Returns:
the resulting string

toString

public static java.lang.String toString(char ch)
Convert a character to a string.

Parameters:
ch - the character to convert
Returns:
the resulting string

toString

public static java.lang.String toString(byte value)
Convert a byte to a string.

Parameters:
value - the value to convert
Returns:
the resulting string

toString

public static java.lang.String toString(short value)
Convert a short to a string.

Parameters:
value - the value to convert
Returns:
the resulting string

toString

public static java.lang.String toString(int value)
Convert an integer to a string.

Parameters:
value - the value to convert
Returns:
the resulting string

toString

public static java.lang.String toString(long value)
Convert a long to a string.

Parameters:
value - the value to convert
Returns:
the resulting string

toString

public static java.lang.String toString(float value)
Convert a float to a string.

Parameters:
value - the value to convert
Returns:
the resulting string

toString

public static java.lang.String toString(double value)
Convert a double to a string.

Parameters:
value - the value to convert
Returns:
the resulting string

toString

public static java.lang.String toString(boolean value)
Convert a boolean to a string.

Parameters:
value - the value to convert
Returns:
the resulting string

toString

public static final java.lang.String toString(java.lang.Object value,
                                              int max)
Convert a object to its string representation. This function takes care of null objects.

Parameters:
value - the value to be converted
Returns:
the string representing the object

toString

public static final java.lang.String toString(int value,
                                              int digits,
                                              char fill)
Convert a integer to its string representation and fill with characters on left to match the number of digits required. If the number cannot fit in the specified digits, then the return string is only composed of '*'.

Parameters:
value - the value to be converted
digits - the number of desired digits
fill - the character used to fill
Returns:
the string representing the integer

toStringArray

public static java.lang.String[] toStringArray(java.util.Collection strings)
Converts a collection full of Strings to a string array.


toStringArray

public static final java.lang.String toStringArray(java.lang.Object value,
                                                   int used,
                                                   int max)

repeat

public static final java.lang.String repeat(char toRepeat,
                                            int count)
Create a new string by repeating a character.

Parameters:
toRepeat - the character to repeat
count - the number of repetition
Returns:
the resulting string

repeat

public static final java.lang.String repeat(java.lang.String toRepeat,
                                            int count)
Create a new string by repeating a string.

Parameters:
toRepeat - the string to repeat
count - the number of repetition
Returns:
the resulting string

replaceFirst

public static final java.lang.String replaceFirst(java.lang.String source,
                                                  java.lang.String value,
                                                  java.lang.String replace)
Replace the first occurrence of a string within a source string.

Parameters:
source - the source string
value - the string to search for
replace - the string replacement
Returns:
the resulting string

replace

public static final java.lang.String replace(java.lang.String source,
                                             java.lang.String value,
                                             java.lang.String replace)
Replace all the occurrences of a string within a source string.

Parameters:
source - the source string
value - the string to search for
replace - the string replacement
Returns:
the resulting string

replace

public static final java.lang.String replace(java.lang.String string,
                                             char value,
                                             char replace)
Replace all the occurrences of a character within a source string.

Parameters:
source - the source string
value - the character to search for
replace - the character replacement
Returns:
the resulting string

toUnsignedHex

public static final java.lang.String toUnsignedHex(int value,
                                                   int nChars)
Convert an integer to an Hexa string and pad the result with '0'

Parameters:
value - the int to convert
nChars - the number of characters of the result
Returns:
the resulting string
Throws:
java.lang.NumberFormatException - if nChars is less that the actual number of characters needed

hexChar

public static final char hexChar(int v)

hexValue

public static final int hexValue(char c)

splitString

public static java.lang.String[] splitString(java.lang.String s,
                                             char sep,
                                             boolean trim)
Breaks a string into an array of substrings, with the delimeter removed. The delimeter character passed as aparanmeter. Note that java.util.StringTokenizer offers similar functionality. This method differs by the fact that it returns all of the tokens at once, using a String array, and does not require the explicit creation of a tokenizer object. This function also returns empty strings when delimiters are concatened, which is not the case of java.util.StringTokenizer. Finally, the delimiter is unique and passed as a char, which makes the method much faster.

Parameters:
s - the string to split
c - the delimeter
trim - indicates if the strings returned should be trimmed
Returns:
the substrings of s

splitString

public static java.lang.String[] splitString(java.lang.String s,
                                             java.lang.String sep,
                                             boolean trim)

splitString

public static final java.lang.String[] splitString(java.lang.String s,
                                                   char sep)
Breaks a string into an array of substrings, with the delimeter removed. The delimeter character passed as aparanmeter. Note that java.util.StringTokenizer offers similar functionality. This method differs by the fact that it returns all of the tokens at once, using a String array, and does not require the explicit creation of a tokenizer object. This function also returns empty strings when delimiters are concatened, which is not the case of java.util.StringTokenizer. Finally, the delimiter is unique and passed as a char, which makes the method much faster.
The returned strings are *not* trimmed.

Parameters:
s - the string to split
c - the delimeter
Returns:
the substrings of s

ltrim

public static final java.lang.String ltrim(java.lang.String s)
Trims the space characters from the beginning of a string. For example, the call ltrim (" Tennessee") returns the string "Tennessee".

Parameters:
s - the string to edit
Returns:
the trimmed string

rtrim

public static final java.lang.String rtrim(java.lang.String s)
Trims the space characters from the end of a string. For example, the call rtrim ("Tennessee ") returns the string "Tenness".
All characters that have codes less than or equal to '\u0020' (the space character) are considered to be white space.

Parameters:
s - the string to edit
Returns:
the trimmed string

indexOfIgnoreCase

public static int indexOfIgnoreCase(java.lang.String source,
                                    java.lang.String str,
                                    int fromIndex)
Returns the index within this string of the first occurrence of the specified string. The search is done ignore the character case.

Parameters:
source - the string to search
str - the string to search for
fromIndex - the index to start from
Returns:
the index of the first occurrence of the string in the character sequence represented by this object, or -1 if the string does not occur.

indexOfIgnoreCase

public static int indexOfIgnoreCase(java.lang.String source,
                                    java.lang.String str)
Returns the index within this string of the first occurrence of the specified string. The search is done ignore the character case.

Parameters:
source - the string to search
str - the string to search for
Returns:
the index of the first occurrence of the string in the character sequence represented by this object, or -1 if the string does not occur.

lastIndexOfIgnoreCase

public static int lastIndexOfIgnoreCase(java.lang.String source,
                                        java.lang.String str,
                                        int fromIndex)
Returns the index within this string of the last occurrence of the specified string. The search is done ignore the character case.

Parameters:
source - the string to search
str - the string to search for
fromIndex - the index to start from
Returns:
the index of the first occurrence of the string in the character sequence represented by this object, or -1 if the string does not occur.

lastIndexOfIgnoreCase

public static int lastIndexOfIgnoreCase(java.lang.String source,
                                        java.lang.String str)
Returns the index within this string of the last occurrence of the specified string. The search is done ignore the character case.

Parameters:
source - the string to search
str - the string to search for
Returns:
the index of the first occurrence of the string in the character sequence represented by this object, or -1 if the string does not occur.

startsWithIgnoreCase

public static boolean startsWithIgnoreCase(java.lang.String s,
                                           java.lang.String prefix)
Identical to String.startsWith but case insensitive.


startsWithIgnoreCase

public static boolean startsWithIgnoreCase(java.lang.String s,
                                           java.lang.String prefix,
                                           int start)
Identical to String.startsWith but case insensitive.


endsWithIgnoreCase

public static boolean endsWithIgnoreCase(java.lang.String s,
                                         java.lang.String suffix)
Identical to String.endsWith but case insensitive.


isEncrypted

public static boolean isEncrypted(java.lang.String pwd)
Deprecated. 


encrypt

public static java.lang.String encrypt(java.lang.String pwd)
Deprecated. 


encrypt

public static java.lang.String encrypt(java.lang.String pwd,
                                       java.lang.String ctx)
Deprecated. 


decrypt

public static java.lang.String decrypt(java.lang.String pwd)
Deprecated. 


fromXmlInputString

public static java.lang.String fromXmlInputString(java.lang.String s)
Converts an HTML string to a Java one, converting the numeric entities (Ӓ) sent by the browser. this does not handle HTML strings, just XML strings. i.e.   is handled but   is not.

Parameters:
s - the HTML string
Returns:
the converted string

isTrueValue

public static boolean isTrueValue(java.lang.String stringToTest)

isFalseValue

public static boolean isFalseValue(java.lang.String stringToTest)

getProperCaseString

public static java.lang.String getProperCaseString(java.lang.String sample)
Returns a string whose first letter is a Capital letter, and a space is inserted anywhere an upper case letter exists in the existing string. For instance, if the user passes in thisIsATest - the following will be returned: This Is A Test

Parameters:
sample: - String - The string that you want to convert to proper case.
Returns:
String

getLineSeparators

public static java.lang.String[] getLineSeparators()

removeLineBreaks

public static java.lang.String removeLineBreaks(java.lang.String s)
Remove line breaks.


parseHtml

public static java.lang.String parseHtml(java.lang.String s)
Strip extra spaces and html tags.


getAutoGenNameFromJavaName

public static java.lang.String getAutoGenNameFromJavaName(java.lang.String javaName)

getMnemonicCharacter

public static char getMnemonicCharacter(java.lang.String text)

generateCopyName

public static java.lang.String generateCopyName(java.lang.String sourceName,
                                                java.lang.String[] listOfNames)
Create a name with the _copy_x appended, where X is the next available number.


getNextUniqueValue

public static java.lang.String getNextUniqueValue(java.lang.String name,
                                                  long startingNum,
                                                  java.lang.String[] listOfNames)
Given the current name, append a long until a unique is found, starting at the given starting num.


removeMnemonics

public static java.lang.String removeMnemonics(java.lang.String string)
Remove the mnemonics from a string.

Parameters:
string -
Returns:
If no mnemonics are found, the string is returned as is. If mnemonics are found, a new string without the mnemonics is returned.

removeMnemonics

public static java.lang.String removeMnemonics(java.lang.String string,
                                               boolean remove)
A Helper function for conditionally calling removeMnemonics. Used in the case where the mnemonic is conditionally removed based on if the control is in a dialog or not.

Parameters:
string -
remove - - if true remove any mnemonics in the string, if false just return the string.
Returns:

getDigest

public static java.lang.String getDigest(java.lang.String str)
Transforms a String into a fixed-length digest. This is a one-way transformation, but with consistent return value.

Parameters:
str - the String to transform
Returns:
the digest in hexadecimal form

toHexValue

public static java.lang.String toHexValue(byte[] barr)
Converts a byte array to a hexadecimal String equivalent.


contains

public static boolean contains(java.lang.Object[] objects,
                               java.lang.Object toTest)

isLineSeparator

public static boolean isLineSeparator(java.lang.String s)

isEmpty

public static boolean isEmpty(java.lang.String[] arr)
Checks if an array of string is empty.

An array is empty if it is nul, or if it contains no elements.

Parameters:
arr -
Returns:

isNotEmpty

public static boolean isNotEmpty(java.lang.String[] arr)
Checks if an array of string is not empty.

An array is empty if it is nul, or if it contains no elements.

Parameters:
arr -
Returns:

isContainedWithin

public static boolean isContainedWithin(java.lang.String s,
                                        java.lang.String[] container)

expandProperties

public static java.lang.String expandProperties(java.lang.String s)
Property expander This function replaces all the occurences of ${propname} by the actual property value

Parameters:
s -
flag -
Returns: