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 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 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 int[] getQuotedBoundaries(String sText)
          Returns the boundaries of quoted strings.
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 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 replace(String pText, String pOld, String pNew)
          Returns a new string resulting from replacing all occurrences of pOld in this string with pNew.
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 toString(Object pObject)
          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

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

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

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

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)

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

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


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.