com.quadrique.jbuildingblocks.core.string
Class JbbString

java.lang.Object
  extended by com.quadrique.jbuildingblocks.core.string.JbbString

public class JbbString
extends Object

This class provides convenience routines to manipulate String instances

Version:
1.0.0
Author:
Hervé Rivere

Method Summary
 int compare(String b)
          compares this string to b ,assumes null as less than non null value.
 boolean containsAtLeastOneDigit()
           
 boolean containsAtLeastOneDigitAndAtLeastOneLetter()
           
 boolean containsAtLeastOneLetter()
           
 boolean containsTokens(String zTokens)
           
 void convertFirstLetterToUpperCase()
          the string with a first letter in upper case
 void convertFirstLetterToUpperCaseAndReminderToLowerCase()
          the string with a first letter in upper case and the other letters in lower case.
 double convertToDouble()
          A more robust conversion routine than Double.parseDouble().
 float convertToFloat()
          A more robust conversion routine than Float.parseFloat().
 void convertToHtml()
          replaces \n to
\t to     
 int convertToInt()
          A more robust conversion routine than Integer.parseInt().
 long convertToNumericVersionNumber()
          Assumed to be of the form xxx.yyy.zzz where xxx ranges from 0..999, yyy ranges from 0...999 and zzz ranges from 0 to 999
 void convertToPortableFileName()
          This method takes an input String and convert it to a portable file name i.e.
 void convertToUnicodeBasicLatinAscii(boolean replaceWithClosestAsciiMatchIfAvailable)
          This method returns a new String which contains: if replaceWithClosestAsciiMatchIfAvaillable is false, this method returns a new String which only contains the characters in the input string that are in the Unicode range from 0000 to 007F (i.e.
 String convertToWwwFormUrlEncoded()
           
 void convertXmlToPrettyHtml()
           
 int countNumberOfOccurences(char zChar)
           
 boolean doesNotContainTokens(String zTokens)
           
 void extractFromTheLastCharacterOccurenceToTheEnd(char zDelimiterChar, boolean zRemoveDelimiterChar)
          To remove all the characters from the last occurence of the given delimiter to the end of the string For instance, removeFromLastCharacterOccurenceToTheEnd(" this is cool",'i',true) returns " this " For instance, removeFromLastCharacterOccurenceToTheEnd(" this is cool",'c',false) returns " this is c" For instance, removeFromLastCharacterOccurenceToTheEnd(" this is cool",'z',false) returns " this is cool"
 ArrayList<String> extractTokens(String zDelimiters)
          extract the tokens from the given string.
 ArrayList<String> extractTokensIncludingEmptyValues(String zDelimiters)
          extract the tokens from the given string.
 String getString()
           
 boolean isAlphaNum()
           
 boolean isAlphaNumUnderscore()
           
 boolean isCharacterInCharSet(char zChar, String zCharSet)
           
 boolean isCharInCharSet(char zChar, String zCharSet)
           
 boolean isFrenchCellPhoneNumber()
          phoneNb in the form "06-23-45-67-89" or "0623456789" or "06 23 45 67 89"
 boolean isFrenchPhoneNumber()
          phoneNb in the form "01-23-45-67-89" or "0123456789" or "01 23 45 67 89"
 boolean isNotEmpty()
          Makes sure the given string is not null and is not of 0 length
 void isNotEmpty(String zValueName)
          Makes sure the given string is not null and is not of 0 length
 boolean isNotEmptyAndIsNotWhiteSpace()
          Makes sure the given string is not null, is not of 0 length and that it contains at least one non white space character
 void isNotEmptyAndIsNotWhiteSpace(String zValueName)
          Makes sure the given string is not null, is not of 0 length and that it contains at least one non white space character
 boolean isNum()
           
 boolean isStringInCharSet(String zCharSet)
           
 boolean isStringInStringSet(HashSet<String> zStringSet)
          To determine if a given string is part of a given set of strings.
 boolean isUsPhoneNumber()
          phoneNb in the form "870-345-4567" or "8703454567" or "870 345 4567"
 void isValidEmailAddress()
          Returns if the email address is valid.
 void isValidIpAddress()
          Returns if the IP address is in a proper dotted decimal format: X1.X2.X3.X4 where Xi is [0...255] the IP address in dotted decimal format (e.g.
 boolean isWhiteSpace()
           
 boolean matchPattern(String zRegularExpression)
          make sure that the given string satisfies the given regular expression
static JbbString newInstance()
           
static JbbString newInstance(String str)
           
 void padBeginning(int zDesiredNbOfChars)
           
 void padBeginning(int zDesiredNbOfChars, char zPadChar)
           
 void padEnding(int zDesiredNbOfChars)
           
 void padEnding(int zDesiredNbOfChars, char zPadChar)
           
 void removeCharacter(char zChar)
          To remove the given character from the given string
 void removeCharacters(String zCharSet)
          To remove the given characters from the given string
 void removeCharactersNotInCharset(String zCharSet)
          To remove the characters that are no in the given characters set from the given string
 void removeFromTheBeginningToTheFirstCharacterOccurence(char zDelimiterChar, boolean zRemoveDelimiterChar)
          To remove all the characters from the beginning of the string to the first occurrence of the given character.
 void removeFromTheBeginningToTheFirstStringOccurence(String delimiterString, boolean removeDelimiterString)
          To remove all the characters from the beginning of the string to the first occurrence of the given delimiter string.
 void removeFromTheLastCharacterOccurenceToTheEnd(char zDelimiterChar, boolean zRemoveDelimiterChar)
          To remove all the characters from the last occurence of the given delimiter to the end of the string For instance, removeFromLastCharacterOccurenceToTheEnd(" this is cool",'i',true) returns " this " For instance, removeFromLastCharacterOccurenceToTheEnd(" this is cool",'c',false) returns " this is c" For instance, removeFromLastCharacterOccurenceToTheEnd(" this is cool",'z',false) returns " this is cool"
 void removeNewLineCharacters()
          To remove the end of line/new line characters (\n\r) from the given string
 void removeStringFromString(String stringToRemove)
          To remove all occurrences of the specified text from the given string.
 void replaceAnySingleQuoteByTwoSingleQuotes()
          This method is very helpful whenever you are handling SQL statements for a database.
 void replaceCharacter(char zOldChar, char zNewChar)
          To replace a given character with another in the given string
 void replaceCharacterByString(char charToReplace, String replacementString)
          To replace a given character with another in the given string
 void replaceCharacters(String charsToReplace, char replacementChar)
          To replace the given characters in the given string with a new character
 void replaceFromTheBeginningToTheFirstStringOccurence(String delimiterString, boolean removeDelimiterString, String replacementString)
          To replace all the characters from the beginning of the string to the first occurrence of the given delimiter string by a new prefix string.
 String toString()
           
 void trim()
           
 void trim(int maxSize)
           
 void trim(String zTrimChars)
           
 void trimBeginningOnly()
           
 void trimBeginningOnly(String zTrimChars)
           
 void trimEndingOnly()
           
 void trimEndingOnly(String zTrimChars)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

newInstance

public static JbbString newInstance()
Returns:
a JbbString instance

newInstance

public static JbbString newInstance(String str)
Returns:
a JbbString instance initialized with the given String

getString

public String getString()
Returns:
string

toString

public String toString()
Overrides:
toString in class Object
Returns:
string

extractTokens

public ArrayList<String> extractTokens(String zDelimiters)
extract the tokens from the given string. A token is a maximal sequence of consecutive characters that are not delimiters. The delimiters are the characters that separate tokens. For instance, with "," as a delimiter, the string "welcome to java, enjoy it" will be broken into 2 tokens: "welcome to java" and " enjoy it".

Parameters:
zDelimiters - the characters that are the delimiters for separating tokens (e.g. " ,")
Returns:
an ArrayList instance that contains the list of tokens (can be empty). Eeach token is represented as a String instance.

extractTokensIncludingEmptyValues

public ArrayList<String> extractTokensIncludingEmptyValues(String zDelimiters)
                                                    throws Exception
extract the tokens from the given string. A token is a maximal sequence of consecutive characters that are not delimiters. The delimiters are the characters that separate tokens. For instance, with "," as a delimiter, the string "welcome to java, enjoy it" will be broken into 2 tokens: "welcome to java" and " enjoy it".

Parameters:
zDelimiters - the characters that are the delimiters for separating tokens (e.g. " ,")
Returns:
an ArrayList instance that contains the list of tokens (can be empty). Eeach token is represented as a String instance.
Throws:
Exception

isAlphaNum

public boolean isAlphaNum()
                   throws Exception
Returns:
true if the given string only contains alphanum charaters (i.e. a-zA-Z0-9)
Throws:
Exception

isNum

public boolean isNum()
              throws Exception
Returns:
true if the given string only contains num charaters (i.e. 0-9)
Throws:
Exception

isAlphaNumUnderscore

public boolean isAlphaNumUnderscore()
                             throws Exception
Returns:
true if the given string only contains alphanum charaters (i.e. 'a-zA-Z0-9') and underscore(i.e. '_')
Throws:
Exception

isCharacterInCharSet

public boolean isCharacterInCharSet(char zChar,
                                    String zCharSet)
                             throws Exception
Parameters:
zChar - the character to test
zCharSet - the set of valid characters
Returns:
true if the character is one of the characters in the given character set, false otherwise
Throws:
Exception

isStringInCharSet

public boolean isStringInCharSet(String zCharSet)
                          throws Exception
Parameters:
zCharSet - the set of valid characters
Returns:
true if all characters in the string belongs to the given character set
Throws:
Exception

isWhiteSpace

public boolean isWhiteSpace()
                     throws Exception
Returns:
true if all the character(s) in the String are whitespace. A character is a white space if and only if it satisfies one of the following criteria:
  • It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('?', '?', '?').
  • It is ' ', HORIZONTAL TABULATION.
  • It is ' ', LINE FEED.
  • It is ' ', VERTICAL TABULATION.
  • It is ' ', FORM FEED.
  • It is ' ', CARRIAGE RETURN.
  • It is '', FILE SEPARATOR.
  • It is '', GROUP SEPARATOR.
  • It is '', RECORD SEPARATOR.
  • It is '', UNIT SEPARATOR.
Throws:
Exception

isCharInCharSet

public boolean isCharInCharSet(char zChar,
                               String zCharSet)
Parameters:
zChar - the input character
zCharSet - the set of valid characters
Returns:
true if the given char is part of the given set of characters

isStringInStringSet

public boolean isStringInStringSet(HashSet<String> zStringSet)
                            throws Exception
To determine if a given string is part of a given set of strings. For instance, if the set of strings is "blue", "white" and "blue", calling this method with "green" will return false. calling this method with "white" will return true.

Parameters:
zStringSet - the set of valid Strings
Returns:
true if the input String is part of the given set of Strings
Throws:
Exception

isValidIpAddress

public void isValidIpAddress()
                      throws Exception
Returns if the IP address is in a proper dotted decimal format: X1.X2.X3.X4 where Xi is [0...255] the IP address in dotted decimal format (e.g. "127.0.0.1")

Throws:
Exception - if the IP address is not valid.

isNotEmpty

public boolean isNotEmpty()
                   throws Exception
Makes sure the given string is not null and is not of 0 length

Throws:
Exception

isNotEmpty

public void isNotEmpty(String zValueName)
                throws Exception
Makes sure the given string is not null and is not of 0 length

Parameters:
zValueName - the name to use for this string when the exception is launched ("zName..." cannot be null or empty)
Throws:
Exception

isNotEmptyAndIsNotWhiteSpace

public void isNotEmptyAndIsNotWhiteSpace(String zValueName)
                                  throws Exception
Makes sure the given string is not null, is not of 0 length and that it contains at least one non white space character

Parameters:
zValueName - the name to use for this string when the exception is launched ("zName..." cannot be null or empty)
Throws:
Exception

isNotEmptyAndIsNotWhiteSpace

public boolean isNotEmptyAndIsNotWhiteSpace()
                                     throws Exception
Makes sure the given string is not null, is not of 0 length and that it contains at least one non white space character

Throws:
Exception

isValidEmailAddress

public void isValidEmailAddress()
                         throws JbbInvalidEmailAddressException
Returns if the email address is valid. These are of the form: "local-part@domain"

Throws:
Exception - if the email address is not valid.
JbbInvalidEmailAddressException

removeFromTheBeginningToTheFirstCharacterOccurence

public void removeFromTheBeginningToTheFirstCharacterOccurence(char zDelimiterChar,
                                                               boolean zRemoveDelimiterChar)
                                                        throws Exception
To remove all the characters from the beginning of the string to the first occurrence of the given character. If the given character is not found, this entire string is erased and will be an empty string. For instance, removeFromBeginningToFirstCharacterOccurence(" this is cool",'i',true) returns "s is cool" For instance, removeFromBeginningToFirstCharacterOccurence(" this is cool",'c',false) returns "cool" For instance, removeFromBeginningToFirstCharacterOccurence(" this is cool",'z',false) returns ""

Parameters:
zDelimiterChar - the character that indicates the end of the characters to remove
zRemoveDelimiterChar - true if the delimiter character is to be removed as well
Throws:
Exception

removeFromTheLastCharacterOccurenceToTheEnd

public void removeFromTheLastCharacterOccurenceToTheEnd(char zDelimiterChar,
                                                        boolean zRemoveDelimiterChar)
                                                 throws Exception
To remove all the characters from the last occurence of the given delimiter to the end of the string For instance, removeFromLastCharacterOccurenceToTheEnd(" this is cool",'i',true) returns " this " For instance, removeFromLastCharacterOccurenceToTheEnd(" this is cool",'c',false) returns " this is c" For instance, removeFromLastCharacterOccurenceToTheEnd(" this is cool",'z',false) returns " this is cool"

Parameters:
zDelimiterChar - the character that indicates the beginning of the characters to remove
zRemoveDelimiterChar - true if the delimiter character is to be removed as well
Throws:
Exception

removeCharacter

public void removeCharacter(char zChar)
                     throws Exception
To remove the given character from the given string

Parameters:
zChar - the character to remove
Throws:
Exception

removeCharacters

public void removeCharacters(String zCharSet)
                      throws Exception
To remove the given characters from the given string

Parameters:
zCharSet - the set of characters that should be removed if they are present in the input string
Throws:
Exception

removeCharactersNotInCharset

public final void removeCharactersNotInCharset(String zCharSet)
                                        throws Exception
To remove the characters that are no in the given characters set from the given string

Parameters:
zCharSet - the set of characters that should be kept if they are present in the input string
Throws:
Exception

removeNewLineCharacters

public void removeNewLineCharacters()
                             throws Exception
To remove the end of line/new line characters (\n\r) from the given string

Throws:
Exception

replaceCharacter

public void replaceCharacter(char zOldChar,
                             char zNewChar)
                      throws Exception
To replace a given character with another in the given string

Parameters:
zOldChar - the character to replace
zNewChar - the character to use as a replacement to zOldChar
Throws:
Exception

matchPattern

public boolean matchPattern(String zRegularExpression)
                     throws Exception
make sure that the given string satisfies the given regular expression

Parameters:
zRegularExpression -
Returns:
true if it matches
Throws:
Exception

containsAtLeastOneLetter

public boolean containsAtLeastOneLetter()
Returns:
true if this string contains at least 1 letter, false otherwise

containsAtLeastOneDigit

public boolean containsAtLeastOneDigit()
Returns:
true if this string contains at least 1 digit, false otherwise

containsAtLeastOneDigitAndAtLeastOneLetter

public boolean containsAtLeastOneDigitAndAtLeastOneLetter()
Returns:
true if this string contains at least 1 digit and 1 letter, false otherwise

isFrenchPhoneNumber

public boolean isFrenchPhoneNumber()
                            throws Exception
phoneNb in the form "01-23-45-67-89" or "0123456789" or "01 23 45 67 89"

Returns:
true if it is a vaid French phone number
Throws:
exception
Exception

isFrenchCellPhoneNumber

public boolean isFrenchCellPhoneNumber()
                                throws Exception
phoneNb in the form "06-23-45-67-89" or "0623456789" or "06 23 45 67 89"

Returns:
true if it is a vaid French phone number
Throws:
exception
Exception

isUsPhoneNumber

public boolean isUsPhoneNumber()
                        throws Exception
phoneNb in the form "870-345-4567" or "8703454567" or "870 345 4567"

Returns:
true if it is a vaid US phone number
Throws:
exception
Exception

countNumberOfOccurences

public int countNumberOfOccurences(char zChar)
Parameters:
zChar - the character whose occurrences in this string have to be counted
Returns:
the number of occurrences of the character zChar in the string zInputStr Example: zInputStr = "127.0.0.1" zChar = '.' ==> returns 3

containsTokens

public boolean containsTokens(String zTokens)
Parameters:
zTokens - a comma delimited set of tokens (e.g. "token1,token2, token3 ")
Returns:
true if this string contains all the given tokens, false otherwise

doesNotContainTokens

public boolean doesNotContainTokens(String zTokens)
Parameters:
zTokens - a comma delimited set of tokens (e.g. "token1,token2, token3 ")
Returns:
true if this string does not contain any of the given tokens, false otherwise

convertToNumericVersionNumber

public final long convertToNumericVersionNumber()
                                         throws Exception
Assumed to be of the form xxx.yyy.zzz where xxx ranges from 0..999, yyy ranges from 0...999 and zzz ranges from 0 to 999

Returns:
a unique version number equal to 1,000,0000*xxx + 1,000*yyy + zzz (e.g. "2.2.0" is converted to the 2002000 integer value)
Throws:
Exception

trim

public void trim(String zTrimChars)
Parameters:
zTrimChars - the character to trim from the input string (e.g. " ")

trim

public void trim()

trimBeginningOnly

public void trimBeginningOnly(String zTrimChars)
Parameters:
zTrimChars - the characters to trim from the input string (e.g. " ")

trimBeginningOnly

public void trimBeginningOnly()

trimEndingOnly

public void trimEndingOnly(String zTrimChars)
Parameters:
zTrimChars - the character(s) to trim from the end of the input string (e.g. " *")

trimEndingOnly

public void trimEndingOnly()

padBeginning

public void padBeginning(int zDesiredNbOfChars,
                         char zPadChar)
                  throws Exception
Parameters:
zDesiredNbOfChars -
zPadChar - the character to pad
Throws:
Exception

padBeginning

public void padBeginning(int zDesiredNbOfChars)
                  throws Exception
Parameters:
zDesiredNbOfChars -
Throws:
Exception

padEnding

public void padEnding(int zDesiredNbOfChars,
                      char zPadChar)
               throws Exception
Parameters:
zDesiredNbOfChars -
zPadChar - the padding character (e.g. ' ')
Throws:
Exception

padEnding

public void padEnding(int zDesiredNbOfChars)
               throws Exception
Parameters:
zDesiredNbOfChars -
Throws:
Exception

convertXmlToPrettyHtml

public final void convertXmlToPrettyHtml()

convertToHtml

public final void convertToHtml()
replaces \n to
\t to     


replaceAnySingleQuoteByTwoSingleQuotes

public void replaceAnySingleQuoteByTwoSingleQuotes()
                                            throws Exception
This method is very helpful whenever you are handling SQL statements for a database.
For instance, the dynamic SQL statement:
"SELECT UserId FROM Users_t WHERE UserName='" + str1 + "'""
works when str1 is "doe" or "dos santos" but it doesn't work for "o'reilly" as the single quote character ("'") is a SQL delimiter.
the solution is to replace every single quote character by two single quote characters. For instance, :
"SELECT UserId FROM Users_t WHERE UserName='o''reilly'"

Throws:
Exception

trim

public void trim(int maxSize)
Parameters:
maxSize -

replaceCharacters

public void replaceCharacters(String charsToReplace,
                              char replacementChar)
                       throws Exception
To replace the given characters in the given string with a new character

Parameters:
charsToReplace - the set of characters (e.g. " '") that should be replaced if they are present in the input string
replacementChar - the replacment character
Throws:
Exception

convertToInt

public final int convertToInt()
                       throws Exception
A more robust conversion routine than Integer.parseInt(). For instance, if the input String is "3.0", "3" will be returned. A null or empty string returns 0 as opposed to generating an Exception.

Throws:
Exception

convertToFloat

public final float convertToFloat()
                           throws Exception
A more robust conversion routine than Float.parseFloat(). For instance, if a null or empty string is provided, "0.0" is returned as opposed to generating an Exception.

Returns:
float
Throws:
Exception

convertToDouble

public final double convertToDouble()
                             throws Exception
A more robust conversion routine than Double.parseDouble(). For instance, if a null or empty string is provided, "0.0" is returned as opposed to generating an Exception.

Returns:
double
Throws:
Exception

convertToUnicodeBasicLatinAscii

public final void convertToUnicodeBasicLatinAscii(boolean replaceWithClosestAsciiMatchIfAvailable)
                                           throws Exception
This method returns a new String which contains:

Parameters:
replaceWithClosestAsciiMatchIfAvailable -
Throws:
Exception

convertToPortableFileName

public final void convertToPortableFileName()
                                     throws Exception
This method takes an input String and convert it to a portable file name i.e. a file name that works on all platforms (Microsoft, Unix, Mac OS X...) The method first converts the input String to an ASCII string (using the convertToUnicodeBasicLatinAscii() method) and it then removes all non valid characters in a file name (e.g. '"\/ ... and '-' or '_' as the first character of a file name)

Throws:
Exception

extractFromTheLastCharacterOccurenceToTheEnd

public void extractFromTheLastCharacterOccurenceToTheEnd(char zDelimiterChar,
                                                         boolean zRemoveDelimiterChar)
                                                  throws Exception
To remove all the characters from the last occurence of the given delimiter to the end of the string For instance, removeFromLastCharacterOccurenceToTheEnd(" this is cool",'i',true) returns " this " For instance, removeFromLastCharacterOccurenceToTheEnd(" this is cool",'c',false) returns " this is c" For instance, removeFromLastCharacterOccurenceToTheEnd(" this is cool",'z',false) returns " this is cool"

Parameters:
zDelimiterChar - the character that indicates the beginning of the characters to remove
zRemoveDelimiterChar - true if the delimiter character is to be removed as well
Throws:
Exception

convertFirstLetterToUpperCaseAndReminderToLowerCase

public void convertFirstLetterToUpperCaseAndReminderToLowerCase()
                                                         throws Exception
the string with a first letter in upper case and the other letters in lower case. For instance, the method returns "Bravo" when called with "bravo" as an input

Throws:
Exception

convertFirstLetterToUpperCase

public void convertFirstLetterToUpperCase()
                                   throws Exception
the string with a first letter in upper case

Throws:
Exception

removeStringFromString

public void removeStringFromString(String stringToRemove)
                            throws Exception
To remove all occurrences of the specified text from the given string.

Parameters:
stringToRemove - the text to be removed
Throws:
Exception

replaceCharacterByString

public void replaceCharacterByString(char charToReplace,
                                     String replacementString)
                              throws Exception
To replace a given character with another in the given string

Parameters:
charToReplace - the character to replace
replacementString - the character to use as a replacement to zOldChar
Throws:
Exception

compare

public int compare(String b)
compares this string to b ,assumes null as less than non null value. Can be used instead of a.compareTo(b) when b could be null..

Parameters:
b -
Returns:
integer value

removeFromTheBeginningToTheFirstStringOccurence

public void removeFromTheBeginningToTheFirstStringOccurence(String delimiterString,
                                                            boolean removeDelimiterString)
                                                     throws Exception
To remove all the characters from the beginning of the string to the first occurrence of the given delimiter string.
If the given string is not found, this entire string is erased and will be an empty string.
For instance:

Parameters:
delimiterString - the String that indicates the end of the characters to remove
removeDelimiterString - true if the delimiter character is to be removed as well
Throws:
Exception

replaceFromTheBeginningToTheFirstStringOccurence

public void replaceFromTheBeginningToTheFirstStringOccurence(String delimiterString,
                                                             boolean removeDelimiterString,
                                                             String replacementString)
                                                      throws Exception
To replace all the characters from the beginning of the string to the first occurrence of the given delimiter string by a new prefix string.
If the given string is not found, this entire string is erased and will be an empty string.
For instance:

Parameters:
delimiterString - the String that indicates the end of the characters to remove
removeDelimiterString - true if the delimiter string is to be removed as well
replacementString -
Throws:
Exception

convertToWwwFormUrlEncoded

public final String convertToWwwFormUrlEncoded()
                                        throws Exception
Throws:
Exception


Copyright © 2001-2008 Quadrique Corporation. All Rights Reserved.