com.sibvisions.util.type
Class StringUtil

java.lang.Object
  extended by com.sibvisions.util.type.StringUtil

public final class StringUtil
extends Object

The StringUtil contains String dependent utility methods.


Nested Class Summary
static class StringUtil.CaseSensitiveType
          case senstive types.
static class StringUtil.CharacterType
          the character types.
static class StringUtil.TextType
          the text types.
 
Method Summary
static String concat(String pDelimiter, List<? extends Object> pObjects)
          Concatenates the given List of Objects with the given delimiter.
static String concat(String pDelimiter, Object... pObjects)
          Concatenates the Objects with the given delimiter.
static String concat(String pDelimiter, String... pElements)
          Concatenates the elements with pDelimiter as separator.
static boolean containsWhitespace(String pText)
          Gets whether a text contains at least one whitespace character.
static String convertMemberNameToText(String pMemberName)
          Converts a member name in a human readable format e.g.
static String convertMemberNameToText(String pMemberName, boolean pReplaceSpecialCharacterSequences)
          Converts a member name in a human readable format e.g.
static String convertMethodNameToFieldName(String pMethodName)
          Converts a method name to a name, without prefix and first character lowercase.
static String convertMethodNameToText(String pMethodName)
          Converts a method name in a human readable format.
static String convertMethodNameToText(String pMethodName, boolean pReplaceSpecialCharacterSequences)
          Converts a method name in a human readable format.
static String convertToMemberName(String pName)
          Converts any text to a member name.
static String convertToMethodName(String pPrefix, String pName)
          Converts any text to a method name.
static String convertToName(String pText)
          Converts text to a valid name.
static int countCharacter(String pText, char pCharToCount)
          Counts the number of times the given character appears in the given text.
static String firstCharLower(String pText)
          Converts the first char to lowercase.
static String firstCharUpper(String pText)
          Converts the first char to uppercase.
static String formatInitCap(String pName)
          Sets the first character in each word to uppercase and the rest to lowercase.
static String formatInitCap(String pName, boolean pRemoveSpaces)
          Sets the first character in each word to uppercase and the rest to lowercase.
static String formatMemberName(String pName)
          Formats a member name.
static String formatMethodName(String pPrefix, String pName)
          Formats a method name with a given property name and the method prefix.
static StringUtil.CaseSensitiveType getCaseSensitiveType(String pText)
          Gets the case sensitive type of a text.
static StringUtil.CharacterType getCharacterType(String pText)
          Gets the character type of a text.
static String getFirstWord(String pText)
          Gets the first word of a text.
static int[] getQuotedBoundaries(String pText)
          Returns the boundaries of quoted strings.
static String getShortenedWords(String pText, int pMaxWordLength)
          Gets all words from the given text with the given maximum length.
static String getText(String pText, char... pAllowed)
          Gets only specific characters from a string.
static String getText(String pText, StringUtil.TextType pType)
          Gets only specific characters from a string.
static boolean isEmpty(String pString)
          Gets whether the given text contains no characters or consists only of whitespace.
static int levenshteinDistance(String pStringA, String pStringB)
          Calculates the Damerau-Levenshtein distance between two strings.
static boolean like(String pSource, String pSearch)
          Fast like search in Strings with wildcard (* and ?)
static String lpad(String pText, int pLength)
          Pads the given string on the left side with spaces until it has the specified length.
static String ltrim(String pText)
          Removes all whitespaces before the first non whitespace character.
static String padLeft(Object pText, int pLength)
          Deprecated. since 2.5, use lpad(String, int) instead.
static String padRight(Object pText, int pLength)
          Deprecated. since 2.5, use rpad(String, int) instead.
static int[] parseColor(String pValue)
          Parses color codes from given string.
static int[] parseInteger(String pValues, String pDelimiter)
          Gets the int values from a string with delimiters.
static String quote(String pText, char pQuoteChar)
          Adds a quote character to the begin and end of text.
static String removeCharacters(String pText, char... pCharsToRemove)
          Removes specific characters from the given string.
static String removeQuotes(String pText, String pQuote)
          Removes the begin and end quote of strings, e.g.
static String removeQuotes(String pText, String pStartQuote, String pEndQuote)
          Removes the begin and end quote of strings, e.g.
static String removeWhitespaces(String pString)
          Removes all the whitespace characters from the given string.
static String removeWhiteSpaces(String pText)
          Deprecated. since 2.5, use removeWhitespaces(String) instead.
static String replace(String pText, String pOld, String pNew)
          Returns a new string resulting from replacing all occurrences of pOld in this string with pNew.
static String replacePlaceholder(String pValue)
          Replaces all placeholders with the values from the defined property.
static String rpad(String pText, int pLength)
          It formats a string with right Padding over pWidth characters.
static String rtrim(String pText)
          Removes all whitespaces after the last non whitespace character.
static String sanitizeId(String pId)
          Sanitizes the given input so that it can be used as ID (for example in a HTML document).
static ArrayUtil<String> separate(String pText, String pStartDelimiter, String pEndDelimiter, boolean pIncludeDelimiter)
          Separates the given text in parts.
static ArrayUtil<String> separateList(String pList, String pDelimiter, boolean pTrim)
          Separates a string of values with a configurable delimiter.
static String stripTags(String pText)
          Strips any tags from the given text.
static String toString(Object pObject)
          Returns a string representation of the "deep contents" of the specified object.
static String toString(Object pObject, int pMaxArrayLength)
          Returns a string representation of the "deep contents" of the specified object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

concat

public static String concat(String pDelimiter,
                            List<? extends Object> pObjects)
Concatenates the given List of Objects with the given delimiter. Object.toString() is used to convert the Object to a String, if the Object is null, the String "null" will be appended.

Parameters:
pDelimiter - the delimiter to use.
pObjects - the Objects to concatenate.
Returns:
the concatenated String. An empty String if the List is either null or empty.
See Also:
concat(String, Object...), concat(String, String...), Object.toString()

concat

public static String concat(String pDelimiter,
                            Object... pObjects)
Concatenates the Objects with the given delimiter. Object.toString() is used to convert the the Object to a String, if the Object is null, the String "null" will be appended.

Parameters:
pDelimiter - the delimiter to use.
pObjects - the Objects to concatenate.
Returns:
the concatenated String. An empty String if the List is either null or empty.
See Also:
concat(String, List), concat(String, String...), Object.toString()

concat

public static String concat(String pDelimiter,
                            String... pElements)
Concatenates the elements with pDelimiter as separator.

Parameters:
pDelimiter - the delimiter.
pElements - the elements to concatenate.
Returns:
the concatenated string.
See Also:
concat(String, List), concat(String, Object...)

containsWhitespace

public static boolean containsWhitespace(String pText)
Gets whether a text contains at least one whitespace character.

Parameters:
pText - the text to check.
Returns:
true if at least one whitespace character was found, false otherwise.
See Also:
Character.isWhitespace(char)

convertMemberNameToText

public static String convertMemberNameToText(String pMemberName)
Converts a member name in a human readable format e.g. myMethodName is converted to My Method Name. No special character sequences are replaced e.g. ue is not replaced with ü (german umlaut).

Parameters:
pMemberName - the member name.
Returns:
the human readable name.
See Also:
convertMethodNameToText(String, boolean)

convertMemberNameToText

public static String convertMemberNameToText(String pMemberName,
                                             boolean pReplaceSpecialCharacterSequences)
Converts a member name in a human readable format e.g. myMethodName is converted to My Method Name.

Parameters:
pMemberName - the member name.
pReplaceSpecialCharacterSequences - true to replace e.g. ue to ü (german umlaut).
Returns:
the human readable name.

convertMethodNameToText

public static String convertMethodNameToText(String pMethodName)
Converts a method name in a human readable format. The prefix is removed and spaces are inserted where the character case is changed e.g. getMyMethodName is converted to My Method Name. No special character sequences are replaced e.g. ue is not replaced with ü (german umlaut).

Parameters:
pMethodName - the method name.
Returns:
the human readable name.
See Also:
convertMethodNameToText(String, boolean)

convertMethodNameToText

public static String convertMethodNameToText(String pMethodName,
                                             boolean pReplaceSpecialCharacterSequences)
Converts a method name in a human readable format. The prefix is removed and spaces are inserted where the character case is changed e.g. getMyMethodName is converted to My Method Name.

Parameters:
pMethodName - the method name.
pReplaceSpecialCharacterSequences - true to replace e.g. {code ue} to ü (german umlaut).
Returns:
the human readable name.

convertMethodNameToFieldName

public static String convertMethodNameToFieldName(String pMethodName)
Converts a method name to a name, without prefix and first character lowercase.

Parameters:
pMethodName - the method name
Returns:
the name

convertToMemberName

public static String convertToMemberName(String pName)
Converts any text to a member name. If the text contains whitespaces or _ they will be removed. If the text contains special characters like German umlauts, the characters will be replaced by appropriate ASCII characters.

Parameters:
pName - the text.
Returns:
the converted member name.

convertToMethodName

public static String convertToMethodName(String pPrefix,
                                         String pName)
Converts any text to a method name. If the text contains whitespaces or _ they will be removed. If the text contains special characters like German umlauts, the characters will be replaced by appropriate ASCII characters. If the name contains no text, null will be returned.

Parameters:
pPrefix - the method prefix e.g. get, set, is, has, ...
pName - the text.
Returns:
the converted member name.

convertToName

public static String convertToName(String pText)
Converts text to a valid name. All whitespaces will be removed and non ASCII characters will be replaced with ASCII characters, if this is possible.

Parameters:
pText - any text.
Returns:
a valid name.

countCharacter

public static int countCharacter(String pText,
                                 char pCharToCount)
Counts the number of times the given character appears in the given text.

Parameters:
pText - the text.
pCharToCount - the character to search.
Returns:
the number of occurrences of pChar in pText.

firstCharLower

public static String firstCharLower(String pText)
Converts the first char to lowercase.

Parameters:
pText - the text.
Returns:
the text with the first character converter to lower case.

firstCharUpper

public static String firstCharUpper(String pText)
Converts the first char to uppercase.

Parameters:
pText - the text.
Returns:
the text with the first character converter to uppercase.

formatInitCap

public static String formatInitCap(String pName)
Sets the first character in each word to uppercase and the rest to lowercase.

Parameters:
pName - the unformatted text.
Returns:
the formatted text.

formatInitCap

public static String formatInitCap(String pName,
                                   boolean pRemoveSpaces)
Sets the first character in each word to uppercase and the rest to lowercase.

Parameters:
pName - the unformatted or formatted text.
pRemoveSpaces - true to remove whitespace characters from the result.
Returns:
the formatted text.

formatMemberName

public static String formatMemberName(String pName)
Formats a member name. A member starts always with a lower case letter.

Parameters:
pName - the member name.
Returns:
the formatted member name.

formatMethodName

public static String formatMethodName(String pPrefix,
                                      String pName)
Formats a method name with a given property name and the method prefix.

Parameters:
pPrefix - the method prefix, an empty string or null if the method name has no prefix.
pName - the unformatted method name.
Returns:
the formatted method name. The first character is upper case when a prefix is used, and the first character is lower case when no prefix is used. The other characters are unchanged.

getCaseSensitiveType

public static StringUtil.CaseSensitiveType getCaseSensitiveType(String pText)
Gets the case sensitive type of a text.

Parameters:
pText - any text or null.
Returns:
StringUtil.CaseSensitiveType.NoLetter if the text contains no letter or the text is null. StringUtil.CaseSensitiveType.LowerCase if the text contains at least one letter and all available letters are lower case. StringUtil.CaseSensitiveType.UpperCase if the text contains at least one letter and all available letters are upper case. StringUtil.CaseSensitiveType.MixedCase if the text contains at least two letters and the text contains lower and upper case letters.

getCharacterType

public static StringUtil.CharacterType getCharacterType(String pText)
Gets the character type of a text.

Parameters:
pText - any text or null.
Returns:
StringUtil.CharacterType.None if the text is empty or null. StringUtil.CharacterType.Letters if the text only contains letters. StringUtil.CharacterType.Digits if the text only contains digits. StringUtil.CharacterType.LettersDigits if the text only contains letters and digits. StringUtil.CharacterType.LettersSpecial if the text contains letters and other characters but no digits. StringUtil.CharacterType.DigitsSpecial if the text contains digits and other characters but no letters. StringUtil.CharacterType.LettersDigitsWhitespace if the text only contains letters, digits and whitespaces. StringUtil.CharacterType.LettersDigitsSpace if the text only contains letters, digits and spaces. StringUtil.CharacterType.OnlyWhitespace if the text only contains whitespaces. StringUtil.CharacterType.OnlySpecial if the text contains no letters and no digits. StringUtil.CharacterType.All if the text contains letters, digits and other characters.

getFirstWord

public static String getFirstWord(String pText)
Gets the first word of a text. The end of the first word is defined as end of text or if a uppercase letter follows.

Parameters:
pText - the text.
Returns:
the first word.

getQuotedBoundaries

public static int[] getQuotedBoundaries(String pText)
Returns the boundaries of quoted strings. Examples.:
 unitTest("Test", Application.class, " Application.class");
 unitTest("Test\", Application.class", Application.class");
 unitTest("Test", Application.class", Application.class);
 

Parameters:
pText - the text with our without quotes.
Returns:
the boundaries as array [start, stop, start, stop] or an empty array.

getShortenedWords

public static String getShortenedWords(String pText,
                                       int pMaxWordLength)
Gets all words from the given text with the given maximum length. If the given text is null or empty the given text is returned. If the given text does not contain any upper case characters, an empty string is returned. Anything that is not a letter is returned verbatim and assumed to be a word separator. Examples:
 SomeDataWorkScreen, 3          -> SomDatWorScr
 ContractsEducation, 3          -> ConEdu
 ContractsEducation, 5          -> ContrEduca
 com.sibvisions.test.ClassName, 3       -> com.sib.tes.ClaNam
 

Parameters:
pText - the text to process.
pMaxWordLength - the maximum length of the words.
Returns:
all words from the given text with the given maximum length. null if the given text was null, an empty string if the given text was empty or the maximum word length was equal or less than zero.

getText

public static String getText(String pText,
                             char... pAllowed)
Gets only specific characters from a string.

Parameters:
pText - any text.
pAllowed - the allowed characters.
Returns:
the alpha numeric characters.

getText

public static String getText(String pText,
                             StringUtil.TextType pType)
Gets only specific characters from a string.

Parameters:
pText - any text.
pType - the character return type.
Returns:
the alpha numeric characters.

isEmpty

public static boolean isEmpty(String pString)
Gets whether the given text contains no characters or consists only of whitespace.

Parameters:
pString - the string to test.
Returns:
true if pText is null or has 0 characters (whitespaces will be ignored).
See Also:
String.trim()

levenshteinDistance

public static int levenshteinDistance(String pStringA,
                                      String pStringB)
Calculates the Damerau-Levenshtein distance between two strings. The return value is basically the number of operations needed to turn one string into another. An operation in this context is a insertion, deletion, substitution or transposition.

Parameters:
pStringA - the first string. Needs to be not null.
pStringB - the second string. Needs to be not null.
Returns:
the Damerau-Levenshtein distance.
Throws:
IllegalArgumentException - if any of the parameters is null .
See Also:
http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance

like

public static boolean like(String pSource,
                           String pSearch)
Fast like search in Strings with wildcard (* and ?) support.

Parameters:
pSource - any string.
pSearch - search pattern with or without wildcards.
Returns:
true if, and only if, the string matches the pattern.

lpad

public static String lpad(String pText,
                          int pLength)
Pads the given string on the left side with spaces until it has the specified length.

Parameters:
pText - the text to format.
pLength - the length in characters to pad left.
Returns:
the left padded String.
Since:
2.5

ltrim

public static String ltrim(String pText)
Removes all whitespaces before the first non whitespace character.

Parameters:
pText - the text.
Returns:
the left trimmed text.

padLeft

@Deprecated
public static String padLeft(Object pText,
                                        int pLength)
Deprecated. since 2.5, use lpad(String, int) instead.

Pads the given string on the left side with spaces until it has the specified length.

Parameters:
pText - the text to format.
pLength - the length in characters to pad left.
Returns:
the left padded String.

padRight

@Deprecated
public static String padRight(Object pText,
                                         int pLength)
Deprecated. since 2.5, use rpad(String, int) instead.

It formats a string with right Padding over pWidth characters.

Parameters:
pText - the text to format.
pLength - the length in characters to pad right.
Returns:
the right padded String.

parseColor

public static int[] parseColor(String pValue)
Parses color codes from given string. The implementation supports following formats:
 #9988AA (web style)
 #98A (web style)  
 0x9988AA
 127, 127, 127 (without alpha value) 
 127, 127, 127, 0 (with alpha value)
 

Parameters:
pValue - the color definition.
Returns:
the found RGB or RGBA values or null if format isn't supported.

parseInteger

public static int[] parseInteger(String pValues,
                                 String pDelimiter)
Gets the int values from a string with delimiters.

Parameters:
pValues - the string with numbers and delimiters.
pDelimiter - the delimiter.
Returns:
the int values or null if the values are null or empty.

quote

public static String quote(String pText,
                           char pQuoteChar)
Adds a quote character to the begin and end of text. If the text contains the quote character then an additional quote character will be added e.g. "value is ""0"""

Parameters:
pText - the text to protect.
pQuoteChar - the quote character.
Returns:
the protected text.

removeCharacters

public static String removeCharacters(String pText,
                                      char... pCharsToRemove)
Removes specific characters from the given string.

Parameters:
pText - a string.
pCharsToRemove - the characters which should be removed.
Returns:
the string without specified characters.

removeQuotes

public static String removeQuotes(String pText,
                                  String pQuote)
Removes the begin and end quote of strings, e.g. 'text' will be translated to text

Parameters:
pText - the quoted text.
pQuote - the quote character e.g. ' or ".
Returns:
the pText without begin and end quote.

removeQuotes

public static String removeQuotes(String pText,
                                  String pStartQuote,
                                  String pEndQuote)
Removes the begin and end quote of strings, e.g. 'text' will be translated to text

Parameters:
pText - the quoted text.
pStartQuote - the start quote character e.g. ' or " or (.
pEndQuote - the end quote character e.g. ' or " or ).
Returns:
the pText without begin and end quote.

removeWhitespaces

public static String removeWhitespaces(String pString)
Removes all the whitespace characters from the given string.

Parameters:
pString - the string to clean.
Returns:
the cleaned string.
See Also:
Character.isWhitespace(char)

removeWhiteSpaces

@Deprecated
public static String removeWhiteSpaces(String pText)
Deprecated. since 2.5, use removeWhitespaces(String) instead.

Gets a String without any white space. If the text is null an empty string is returned.

Parameters:
pText - the text.
Returns:
the text without white spaces.

replace

public static String replace(String pText,
                             String pOld,
                             String pNew)
Returns a new string resulting from replacing all occurrences of pOld in this string with pNew.

Parameters:
pText - the original text.
pOld - the text to replace.
pNew - the replacement.
Returns:
the resulting string.

replacePlaceholder

public static String replacePlaceholder(String pValue)
Replaces all placeholders with the values from the defined property. Use following syntax: ${sys:propertyname} (replacement with the system property), ${env:parametername} (replacement with the environment parameter) or ${name} (replacement with the system property or the environment parameter if system property wasn't found).

Parameters:
pValue - the value with or without placeholders
Returns:
the value with replaced placeholders

rpad

public static String rpad(String pText,
                          int pLength)
It formats a string with right Padding over pWidth characters.

Parameters:
pText - the text to format.
pLength - the length in characters to pad right.
Returns:
the right padded String.
Since:
2.5

rtrim

public static String rtrim(String pText)
Removes all whitespaces after the last non whitespace character.

Parameters:
pText - the text.
Returns:
the trimmed text.

sanitizeId

public static String sanitizeId(String pId)
Sanitizes the given input so that it can be used as ID (for example in a HTML document). The definition of "sanitize" in this case is taken from the HTML 4 spec:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Parameters:
pId - the id to sanitize.
Returns:
the sanitized id. null if the given id was null.

separate

public static ArrayUtil<String> separate(String pText,
                                         String pStartDelimiter,
                                         String pEndDelimiter,
                                         boolean pIncludeDelimiter)
Separates the given text in parts.

Parameters:
pText - the text.
pStartDelimiter - the start delimiter.
pEndDelimiter - the end delimiter.
pIncludeDelimiter - true to include the delimiters in the result for every found part.
Returns:
all found parts.

separateList

public static ArrayUtil<String> separateList(String pList,
                                             String pDelimiter,
                                             boolean pTrim)
Separates a string of values with a configurable delimiter.

Parameters:
pList - string with values.
pDelimiter - delimiter to separate.
pTrim - true to trim the separated values.
Returns:
list of separated values.

stripTags

public static String stripTags(String pText)
Strips any tags from the given text. This does only remove the tags from the string, it does not perform any sort of parsing or similar, so it does not check for any sort of malformed syntax and simply removes everything that is surrounded by <>. Examples:
 <html>Something</html>                                     Something
 <b>Some</b>thing</b>                                 Something
 <html>This is<br>a<br><br>new line test.</html>          This isanew line test.
 

Parameters:
pText - the test from which to strip all HTML tags.
Returns:
the given text without HTML tags.

toString

public static String toString(Object pObject)
Returns a string representation of the "deep contents" of the specified object. If the object contains other objects as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays, Collections and Maps to strings.

The string representation consists of a list of the object's elements, enclosed in brackets ("[]" or "{}"). Adjacent elements are separated by the characters ", " (a comma followed by a space).

To avoid infinite recursion, if the specified object contains itself as an element, or contains an indirect reference to itself through one or more levels of arrays, the self-reference is converted to the string "#REF#". For example, an array containing only a reference to itself would be rendered as "[#REF#]".

This method returns "null" if the specified object is null.

Parameters:
pObject - the object whose string representation to return
Returns:
a string representation of pObject

toString

public static String toString(Object pObject,
                              int pMaxArrayLength)
Returns a string representation of the "deep contents" of the specified object. If the object contains other objects as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays, Collections and Maps to strings.

The string representation consists of a list of the object's elements, enclosed in brackets ("[]" or "{}"). Adjacent elements are separated by the characters ", " (a comma followed by a space).

To avoid infinite recursion, if the specified object contains itself as an element, or contains an indirect reference to itself through one or more levels of arrays, the self-reference is converted to the string "#REF#". For example, an array containing only a reference to itself would be rendered as "[#REF#]".

This method returns "null" if the specified object is null.

Parameters:
pObject - the object whose string representation to return
pMaxArrayLength - the maximum length of arrays. If the length of an array is exceeded, the elements won't be printed. Instead a placeholder like byte[n] will be printed.
Returns:
a string representation of pObject


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.