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, String... pElements)
          Concats the elements with pDelimiter as seperator.
static boolean containsWhitespace(String pText)
          Gets whether a text contains at least one whitespace character.
static String convertMemberNameToText(String pMember)
          Converts a member name in a human readable format e.g.
static String convertMemberNameToText(String pMember, boolean pReplaceSpecialCharacterSequences)
          Converts a member name in a human readable format e.g.
static String convertMethodNameToText(String pMethod)
          Converts a method name in a human readable format.
static String convertMethodNameToText(String pMethod, 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 vaild name.
static int countCharacter(String pText, char pChar)
          Counts the number of a specific characters in a text.
static String firstCharLower(String pText)
          Lowercase the first character.
static String firstCharUpper(String pText)
          Uppercase the first character.
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 sText)
          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, StringUtil.TextType pType)
          Gets only specific characters from a string.
static boolean isEmpty(String pText)
          Gets whether the given text contains no characters.
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 padLeft(Object pText, int pWidth)
          It formats a string with left Padding over pWidth characters.
static String padRight(Object pText, int pWidth)
          It formats a string with right Padding over pWidth characters.
static int[] parseInteger(String pValues, String pDelimiter)
          Gets the int values from a string with delimiters.
static String quote(String pText, char pQuote)
          Adds a quote character to the begin and end of text.
static String removeCharacters(String pText, char[] pRemove)
          Removes specific characters from a 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 pText)
          Gets a String without any white space.
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 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

getQuotedBoundaries

public static int[] getQuotedBoundaries(String sText)
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:
sText - 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.

Examples:

SomeDataWorkScreen, 3 -> SomDatWorScr ContractsEducation, 3 -< ConEdu ContractsEducation, 5 -< ContrEduca

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.

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

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

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

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

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.
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

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 test

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 test

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.

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 vaild 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

convertMethodNameToText

public static String convertMethodNameToText(String pMethod)
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:
pMethod - the method name
Returns:
the human readable name
See Also:
convertMethodNameToText(String, boolean)

convertMethodNameToText

public static String convertMethodNameToText(String pMethod,
                                             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:
pMethod - the method name
pReplaceSpecialCharacterSequences - true to replace e.g. ue to ü (german umlaut)
Returns:
the human readable name

convertMemberNameToText

public static String convertMemberNameToText(String pMember)
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:
pMember - the member name
Returns:
the human readable name
See Also:
convertMethodNameToText(String, boolean)

convertMemberNameToText

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

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

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

removeCharacters

public static String removeCharacters(String pText,
                                      char[] pRemove)
Removes specific characters from a string.

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

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.

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

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

quote

public static String quote(String pText,
                           char pQuote)
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
pQuote - the quote character
Returns:
the protected text

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 replacemenet
Returns:
the resulting string

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.

isEmpty

public static boolean isEmpty(String pText)
Gets whether the given text contains no characters.

Parameters:
pText - a text
Returns:
true if pText is null or has 0 characters (whitespaces will be ignored)

concat

public static String concat(String pDelimiter,
                            String... pElements)
Concats the elements with pDelimiter as seperator.

Parameters:
pDelimiter - the delimiter.
pElements - the elements to concat.
Returns:
the concatinated string.

padRight

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

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

padLeft

public static String padLeft(Object pText,
                             int pWidth)
It formats a string with left Padding over pWidth characters.

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

containsWhitespace

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

Parameters:
pText - the text
Returns:
true if at least one whitespace character was found, false otherwise

removeWhiteSpaces

public static String removeWhiteSpaces(String pText)
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.

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.

countCharacter

public static int countCharacter(String pText,
                                 char pChar)
Counts the number of a specific characters in a text.

Parameters:
pText - the text
pChar - the character to search
Returns:
the number of occurences of pChar in pText

firstCharUpper

public static String firstCharUpper(String pText)
Uppercase the first character.

Parameters:
pText - the text
Returns:
the text but the first character is guaranteed uppercase

firstCharLower

public static String firstCharLower(String pText)
Lowercase the first character.

Parameters:
pText - the text
Returns:
the text but the first character is guaranteed lowercase


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.