Expresso 5-6

com.jcorporate.expresso.core.misc
Class StringUtil

java.lang.Object
  extended bycom.jcorporate.expresso.core.misc.StringUtil

public final class StringUtil
extends Object

This is class that consists of static methods, utilities for manipulations of java.lang.Strings.

Author:
Michael Nash, Peter Pilgrim, Henri Yves AMAIZO

Field Summary
static String ELLIPSES
          HTML code for ellipses (3 dots, like '...' as one character) used for appending to a truncate() line if necessary
static String EMPTY_STRING
           
 
Constructor Summary
StringUtil()
           
 
Method Summary
static void assertBoolean(String theString, String theMessage)
          Throws an exception if theString can't be mapped to a boolean value.
static void assertNotBlank(String theString, String theMessage)
          Make sure a string is not null.
static String centerPad(String str, int n)
          enclosed the string with padding character.
static String centerPad(String str, int n, String delim)
          Enclosed the string with padding character

author Henri Yves AMAIZO

static Hashtable characterMap()
          Map accent characters with equivalent without accent.
static int compareDecimals(String decim1, String decim2)
          Compare 2 decimals string

author Henri Yves AMAIZO

static int compareIntegers(String int1, String int2)
          Compare 2 integers string.
static String convertCommaToDot(String str)
          Replace all comma by dot

author Henri Yves AMAIZO

static String convertDotToComma(String str)
          Replace all dots byt comma

author Henri Yves AMAIZO

static String createWordWrappedString(String input)
          Take an input string S, break the string into parts of N up to 72 characters long which word wrapped with respective to blank space characters and separated with the newline characters.
static String createWordWrappedString(String input, int fixedLength, String delimiter)
          Take an input string S, break the string into parts of N up to F characters long which word wrapped with respective to blank space characters and separated with the supplied delimiter D.
protected static String getEntityRef(int ch)
          Helper to xmlEscape()
static String getStackTraceAsString(Throwable t)
          A utility method to return an exception stacktrace as a String

Written by Peter Pilgrim Mon Mar 15 23:12:15 GMT 2004

static boolean isAlphaNumeric(String s)
          Check if string is alphanumeric or not.
static boolean isAlphaNumeric(String str, String otherChars)
          Check if string is alphanumeric with addons chararcters or not.
static boolean isBlankOrNull(String str)
          Check if an input string is an empty string, just contains whitespace characters, or is null.
static boolean isDecimal(String s)
          Check if decimal number

author Henri Yves AMAIZO

static int isEmail(String email)
          Check if string pass is a vlaid email address

author Henri Yves AMAIZO

static boolean isInteger(String str)
          Check if integer string

author Henri Yves AMAIZO

static boolean isNumber(String str)
          Check if number author Henri Yves AMAIZO
static boolean isSiren(String siren)
          Check French company siren number.
static boolean isSiret(String siret)
          Check French company siret number

author Henri Yves AMAIZO

static String join(String[] input)
          Concatenate array of string into one string with constant delimiter ", "

Written by Peter Pilgrim Mon Mar 15 23:12:15 GMT 2004

static String join(String[] input, String delimiter)
          Concatenate array of string into one string with delimiter

Written by Peter Pilgrim Mon Mar 15 23:12:15 GMT 2004

static String leftPad(String str, int n)
          Left padding padding character is space

author Henri Yves AMAIZO

static String leftPad(String str, int n, String delim)
          Left padding

author Henri Yves AMAIZO

static void main(String[] args)
           
static String notNull(SerializableString theString)
          Same thing but using a serializable string as the parameter instead
static String notNull(String theString)
          Make sure a string is not null.
static String nullToEmpty(String s)
          Return empty string if parameter is null

author Henri Yves AMAIZO

static String numberToLetter(int number, boolean upperCaseFlag)
          This method is useful for creating lists that use letters instead of numbers, such as a, b, c, d...instead of 1, 2, 3, 4.
static String omitPackages(Class theclass)
          utility to get just name of class
static String omitPackages(Object obj)
          utility to get just name of class
protected static FastStringBuffer printEscaped(char ch, FastStringBuffer fsb)
          Formats a particular character to something workable in xml Helper to xmlEscape()
static String random(int count)
          Create a random string author Henri Yves AMAIZO
static String random(int count, boolean letters, boolean numbers)
          Create a random numeric string where you have control over size, and whether you want letters, numbers, or both.
static String random(int count, char[] set)
          Create a random string author Henri Yves AMAIZO
static String random(int count, int start, int end, boolean letters, boolean numbers)
          Create a random numeric string where you have control over size, and whether you want letters, numbers, as well as ANSI minimum and maximum values of the characters.
static String random(int count, int start, int end, boolean letters, boolean numbers, char[] set)
          Create a random numeric string where you have control over size, and whether you want letters, numbers, as well as ANSI minimum and maximum values of the characters.
static String random(int count, String set)
          Create a random string author Henri Yves AMAIZO
static String randomAlphabetic(int count)
          Create a random character only string author Henri Yves AMAIZO
static String randomAlphanumeric(int count)
          Create a random alpha numeric string author Henri Yves AMAIZO
static String randomAscii(int count)
          Create a random Ascii String author Henri Yves AMAIZO
static String randomNumeric(int count)
          Create a random numeric string author Henri Yves AMAIZO
static String reformatDecimalString(String str)
          Reformat string by converting "," by "."
static String removeAccents(String s)
          Remove from the parameter the accent characters and return the remain string or null if empty

author Henri Yves AMAIZO

static String repeatString(String str, int n)
          Repeat str n time to format another string.
static String replace(String s, String sub, String with)
          replace substrings within string.
static String replaceAll(String text, String key, String value)
          Replace all occurences of key by value in text.
static String replaceString(String text, String str1, String str2)
          Substituate all occurence of str1 by str2 in text

author Henri Yves AMAIZO

static String replaceString(String text, String str1, String str2, int max)
          Replace n occurences of str1 in text by str2.
static String replaceStringOnce(String text, String str1, String str2)
          Substituate once str1 by str2 in text Commentaire Anglais

author Henri Yves AMAIZO

static String reverseString(String str)
          Reverse the String.
static String rightPad(String str, int n)
          Right padding delimiter is space

author Henri Yves AMAIZO

static String rightPad(String str, int n, String delim)
          Right padding with delimiter

author Henri Yves AMAIZO

static boolean startsAndEnds(String s, String search)
          determine whether a string both starts and ends with the given substring
static List string2List(String s)
          Convert string to list

author Henri Yves AMAIZO

static List string2List(String s, String sep)
          Convert string to list using sep separator to divide

author Henri Yves AMAIZO

static List string2List(String s, String sep, int maxSize)
          Convert string to list using sep separator to divide

author Henri Yves AMAIZO

static String substring(String str, int lg)
          return empty string the string is null

author Henri Yves AMAIZO

static String substring(String str, int start, int end)
          return empty string the string is null

author Henri Yves AMAIZO

static String swapCase(String str)
          Reverse the character case in the string

author Henri Yves AMAIZO

static boolean toBoolean(String theString)
          parse a boolean value
static String truncate(String str, int len)
          truncate a string at the given length if necessary, adding an ellipses at the end if truncation occurred; uses ELLIPSES static String from this class
static String unquote(String s)
          remove surrounding quote marks (either "" or ''), only if quotes are at both the beginning and end of the string
static String unUpperFirstChar(String str)
          convert the first character of string in lower case

author Henri Yves AMAIZO

static String upperFirstChar(String str)
          convert the first character of the string upper case

author Henri Yves AMAIZO

static String xmlEscape(String s)
          Formats the string to an XML/XHTML escaped character.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_STRING

public static final String EMPTY_STRING
See Also:
Constant Field Values

ELLIPSES

public static final String ELLIPSES
HTML code for ellipses (3 dots, like '...' as one character) used for appending to a truncate() line if necessary

See Also:
Constant Field Values
Constructor Detail

StringUtil

public StringUtil()
Method Detail

assertNotBlank

public static final void assertNotBlank(String theString,
                                        String theMessage)
Make sure a string is not null. This is a convenience method, but it is not necessarily recommended if you do a lot of processing for theMessage parameter. It is recommended that you assertNotBlank, catch IllegalArgumentException and then throw an appropriate exception with your message.

Parameters:
theString - Any string, possibly null
theMessage - The error message to use if the argument is not null.

notNull

public static final String notNull(String theString)
Make sure a string is not null.

Parameters:
theString - Any string, possibly null
Returns:
An empty string if the original was null, else the original

assertBoolean

public static final void assertBoolean(String theString,
                                       String theMessage)
Throws an exception if theString can't be mapped to a boolean value.

Parameters:
theString - the string to check
theMessage - the message to have in the IllegalArgumentException if the conditions aren't met.

toBoolean

public static final boolean toBoolean(String theString)
parse a boolean value

Parameters:
theString - the string to parse
Returns:
a boolean value parse from input; if input cannot be parsed, return false

notNull

public static String notNull(SerializableString theString)
Same thing but using a serializable string as the parameter instead

Parameters:
theString - The string to check against null
Returns:
theString or an empty string if it is null

numberToLetter

public static String numberToLetter(int number,
                                    boolean upperCaseFlag)
                             throws IllegalArgumentException
This method is useful for creating lists that use letters instead of numbers, such as a, b, c, d...instead of 1, 2, 3, 4. Valid numbers are from 1 to 26, corresponding to the 26 letters of the alphabet. By default, the letter is returned as a lowercase, but if the boolean upperCaseFlag is true, the letter will be returned as an uppercase. Creation date: (5/11/00 12:52:23 PM) author: Adam Rossi

Parameters:
number - The number to convert
upperCaseFlag - True if you want the final data to be uppercase
Returns:
java.lang.String
Throws:
IllegalArgumentException

replace

public static String replace(String s,
                             String sub,
                             String with)
replace substrings within string.

Parameters:
s - The string to work with
sub - The string to substitude the occurances of
with - The string to replace with
Returns:
A processed java.util.String

xmlEscape

public static String xmlEscape(String s)
Formats the string to an XML/XHTML escaped character. Useful for &'s, etc

Parameters:
s - the String to format
Returns:
The escaped formatted String.
See Also:
for example of original code.

printEscaped

protected static FastStringBuffer printEscaped(char ch,
                                               FastStringBuffer fsb)
Formats a particular character to something workable in xml Helper to xmlEscape()

Parameters:
ch - the character to print.
fsb - The FastStringBuffer to add this to.
Returns:
a FastStringBuffer that is modified

getEntityRef

protected static String getEntityRef(int ch)
Helper to xmlEscape()

Parameters:
ch - the character to escape
Returns:
A modified string representing the tanlation of the character or null if there is no translation for it.

truncate

public static String truncate(String str,
                              int len)
truncate a string at the given length if necessary, adding an ellipses at the end if truncation occurred; uses ELLIPSES static String from this class

Parameters:
str - The string to process
len - The maximum length to process the string to.
Returns:
the appropriately trimmed string.

characterMap

public static Hashtable characterMap()
Map accent characters with equivalent without accent.

author Henri Yves AMAIZO

Returns:
Hashtable Character mapping table.

removeAccents

public static String removeAccents(String s)
Remove from the parameter the accent characters and return the remain string or null if empty

author Henri Yves AMAIZO

Parameters:
s - the string to remove the accent characters
Returns:
String

convertCommaToDot

public static String convertCommaToDot(String str)
Replace all comma by dot

author Henri Yves AMAIZO

Parameters:
str - String to change
Returns:
String The result of substitution

compareDecimals

public static int compareDecimals(String decim1,
                                  String decim2)
Compare 2 decimals string

author Henri Yves AMAIZO

Parameters:
decim1 - First string to compare
decim2 - Second string to compare
Returns:
int return 1 if decim1 > decim2
retourne 0 if decim1 == decim2
return -1 if decim1 < decim2

compareIntegers

public static int compareIntegers(String int1,
                                  String int2)
Compare 2 integers string.

author Henri Yves AMAIZO

Parameters:
int1 - first string to compare
int2 - second string to compare
Returns:
int return 1 if decim1 > decim2
return 0 if decim1 == decim2
return -1 if decim1 < decim2

isAlphaNumeric

public static boolean isAlphaNumeric(String s)
Check if string is alphanumeric or not.

author Henri Yves AMAIZO

Parameters:
s - String to check.
Returns:
boolean true if alphanumeric, false if not.

isAlphaNumeric

public static boolean isAlphaNumeric(String str,
                                     String otherChars)
Check if string is alphanumeric with addons chararcters or not.

author Henri Yves AMAIZO

Parameters:
str - string to check
otherChars - extra characters to check with
Returns:
boolean true if parameter string contains only alpha numerics,
plus addons characters and false if not.

isBlankOrNull

public static boolean isBlankOrNull(String str)
Check if an input string is an empty string, just contains whitespace characters, or is null. If it is then return true

Parameters:
str - the input string
Returns:
boolean true if the string is blank or null

isDecimal

public static boolean isDecimal(String s)
Check if decimal number

author Henri Yves AMAIZO

Parameters:
s - string to check.
Returns:
boolean true if the value is decimal number false if not

isEmail

public static int isEmail(String email)
Check if string pass is a vlaid email address

author Henri Yves AMAIZO

Parameters:
email - string to check
Returns:
int 0 if valid address, 1 more than 2 tokens (".", "@")
and 2 if the second token is not "." .

isInteger

public static boolean isInteger(String str)
Check if integer string

author Henri Yves AMAIZO

Parameters:
str - string to check
Returns:
boolean true if all characters is digit

isNumber

public static boolean isNumber(String str)
Check if number author Henri Yves AMAIZO

Parameters:
str - to check
Returns:
boolean

isSiren

public static boolean isSiren(String siren)
Check French company siren number.

author Henri Yves AMAIZO

Parameters:
siren - the company number
Returns:
boolean true if ok.

isSiret

public static boolean isSiret(String siret)
Check French company siret number

author Henri Yves AMAIZO

Parameters:
siret - The siret number
Returns:
boolean true if equivalent to siret number false if not.

nullToEmpty

public static String nullToEmpty(String s)
Return empty string if parameter is null

author Henri Yves AMAIZO

Parameters:
s - the parameter to check
Returns:
String return the parameter if empty or if the string == null

reformatDecimalString

public static String reformatDecimalString(String str)
Reformat string by converting "," by "."

author Henri Yves AMAIZO

Parameters:
str - string to reformat
Returns:
String return reformatted string.

convertDotToComma

public static String convertDotToComma(String str)
Replace all dots byt comma

author Henri Yves AMAIZO

Parameters:
str - string to change
Returns:
String reformatted

replaceAll

public static String replaceAll(String text,
                                String key,
                                String value)
Replace all occurences of key by value in text.

author Henri Yves AMAIZO

Parameters:
text - string in
key - occurence to replace
value - string to use
Returns:
String with the replace value

replaceStringOnce

public static String replaceStringOnce(String text,
                                       String str1,
                                       String str2)
Substituate once str1 by str2 in text Commentaire Anglais

author Henri Yves AMAIZO

Parameters:
text - search and replace in
str1 - to search for
str2 - to replace with
Returns:
String replaced

replaceString

public static String replaceString(String text,
                                   String str1,
                                   String str2)
Substituate all occurence of str1 by str2 in text

author Henri Yves AMAIZO

Parameters:
text - search and replace in
str1 - search for
str2 - replace with
Returns:
String with all values replaced

replaceString

public static String replaceString(String text,
                                   String str1,
                                   String str2,
                                   int max)
Replace n occurences of str1 in text by str2. if n = -1 all occurrences are replaced

author Henri Yves AMAIZO

Parameters:
text - search and replace in
str1 - search for
str2 - replace with
max - int values of occrrences to replace
Returns:
String replaced

string2List

public static List string2List(String s)
Convert string to list

author Henri Yves AMAIZO

Parameters:
s - String comma delimited string to format
Returns:
List

string2List

public static List string2List(String s,
                               String sep)
Convert string to list using sep separator to divide

author Henri Yves AMAIZO

Parameters:
s - String comma delimited string to format
sep - a string containing the seprator characters
Returns:
List

string2List

public static List string2List(String s,
                               String sep,
                               int maxSize)
Convert string to list using sep separator to divide

author Henri Yves AMAIZO

Parameters:
s - String comma delimited string to format
sep - a string containing the seprator characters
maxSize - the maximum size of the list
Returns:
List

unUpperFirstChar

public static String unUpperFirstChar(String str)
convert the first character of string in lower case

author Henri Yves AMAIZO

Parameters:
str - String to un-upper case the first character
Returns:
String

upperFirstChar

public static String upperFirstChar(String str)
convert the first character of the string upper case

author Henri Yves AMAIZO

Parameters:
str - String to make the first character upper.
Returns:
String

repeatString

public static String repeatString(String str,
                                  int n)
Repeat str n time to format another string.

author Henri Yves AMAIZO

Parameters:
str - String to repeat
n - int n repeat
Returns:
String

centerPad

public static String centerPad(String str,
                               int n)
enclosed the string with padding character. Space is padding character

author Henri Yves AMAIZO

Parameters:
str - String str string to center padding
n - int n size of the new string
Returns:
String Result

centerPad

public static String centerPad(String str,
                               int n,
                               String delim)
Enclosed the string with padding character

author Henri Yves AMAIZO

Parameters:
str - String str string to pad with
n - int n size of the final string
delim - String delim padding character
Returns:
String result of the center padding

rightPad

public static String rightPad(String str,
                              int n,
                              String delim)
Right padding with delimiter

author Henri Yves AMAIZO

Parameters:
str - String
n - int size of the final string
delim - padding character
Returns:
String padding string

rightPad

public static String rightPad(String str,
                              int n)
Right padding delimiter is space

author Henri Yves AMAIZO

Parameters:
str - String
n - int size of the new string
Returns:
String

leftPad

public static String leftPad(String str,
                             int n)
Left padding padding character is space

author Henri Yves AMAIZO

Parameters:
str - String
n - int size of the new string
Returns:
String

leftPad

public static String leftPad(String str,
                             int n,
                             String delim)
Left padding

author Henri Yves AMAIZO

Parameters:
str - String
n - int size of the new string
delim - padding character
Returns:
String result

reverseString

public static String reverseString(String str)
Reverse the String.

Parameters:
str - the String to reverse.
Returns:
a reversed string

swapCase

public static String swapCase(String str)
Reverse the character case in the string

author Henri Yves AMAIZO

Parameters:
str - String
Returns:
String

random

public static String random(int count)
Create a random string author Henri Yves AMAIZO

Parameters:
count - size of string.
Returns:
randomly generated string of size count

randomAscii

public static String randomAscii(int count)
Create a random Ascii String author Henri Yves AMAIZO

Parameters:
count - the size of the string
Returns:
randomly generated string of size count

randomAlphabetic

public static String randomAlphabetic(int count)
Create a random character only string author Henri Yves AMAIZO

Parameters:
count - size of string
Returns:
randomly generated string of size count

randomAlphanumeric

public static String randomAlphanumeric(int count)
Create a random alpha numeric string author Henri Yves AMAIZO

Parameters:
count - the size of the string
Returns:
randomly generated string of size count

randomNumeric

public static String randomNumeric(int count)
Create a random numeric string author Henri Yves AMAIZO

Parameters:
count - the size of the final string
Returns:
randomly generated string of size count

random

public static String random(int count,
                            boolean letters,
                            boolean numbers)
Create a random numeric string where you have control over size, and whether you want letters, numbers, or both.

author Henri Yves AMAIZO

Parameters:
count - the size of the string
letters - true if you want letters included
numbers - true if you want numbers included
Returns:
randomly generated string of size count

random

public static String random(int count,
                            int start,
                            int end,
                            boolean letters,
                            boolean numbers)
Create a random numeric string where you have control over size, and whether you want letters, numbers, as well as ANSI minimum and maximum values of the characters. author Henri Yves AMAIZO

Parameters:
count - the size of the string
start - int minimum 'value' of the character
end - maximum 'value' of the character
letters - true if you want letters included
numbers - true if you want numbers included
Returns:
randomly generated string of size count

random

public static String random(int count,
                            int start,
                            int end,
                            boolean letters,
                            boolean numbers,
                            char[] set)
Create a random numeric string where you have control over size, and whether you want letters, numbers, as well as ANSI minimum and maximum values of the characters. author Henri Yves AMAIZO

Parameters:
count - the size of the string
start - int minimum 'value' of the character
end - maximum 'value' of the character
letters - true if you want letters included
numbers - true if you want numbers included
set - the set of possible characters that you're willing to let the string contain. may be null if all values are open.
Returns:
randomly generated string of size count

random

public static String random(int count,
                            String set)
Create a random string author Henri Yves AMAIZO

Parameters:
count - the size of the string
set - the set of characters that are allowed
Returns:
randomly generated string of size count

random

public static String random(int count,
                            char[] set)
Create a random string author Henri Yves AMAIZO

Parameters:
count - the size of the string
set - the set of characters that are allowed
Returns:
randomly generated string of size count

substring

public static String substring(String str,
                               int lg)
return empty string the string is null

author Henri Yves AMAIZO

Parameters:
str - The string to split String
lg - the length to subgstring
Returns:
a substring of parameter str.

substring

public static String substring(String str,
                               int start,
                               int end)
return empty string the string is null

author Henri Yves AMAIZO

Parameters:
str - The string to split String
start - the location to start
end - the end location of the substring
Returns:
a substring of parameter str.

omitPackages

public static String omitPackages(Object obj)
utility to get just name of class

Parameters:
obj - the object of the given class, the name of which will be returned
Returns:
name of class, less any package prefix

omitPackages

public static String omitPackages(Class theclass)
utility to get just name of class

Parameters:
theclass - the given class, the name of which will be returned
Returns:
name of class, less any package prefix

getStackTraceAsString

public static String getStackTraceAsString(Throwable t)
A utility method to return an exception stacktrace as a String

Written by Peter Pilgrim Mon Mar 15 23:12:15 GMT 2004


join

public static String join(String[] input)
Concatenate array of string into one string with constant delimiter ", "

Written by Peter Pilgrim Mon Mar 15 23:12:15 GMT 2004

Parameters:
input - the input array string
Returns:
the joined string

join

public static String join(String[] input,
                          String delimiter)
Concatenate array of string into one string with delimiter

Written by Peter Pilgrim Mon Mar 15 23:12:15 GMT 2004

Parameters:
input - the input array string
delimiter - the delimiter
Returns:
the joined string

createWordWrappedString

public static String createWordWrappedString(String input)
Take an input string S, break the string into parts of N up to 72 characters long which word wrapped with respective to blank space characters and separated with the newline characters. This static call has parameters set up for electronic mail standards.

Written by Peter Pilgrim Mon Mar 15 23:12:15 GMT 2004

Parameters:
input - the input string
Returns:
wrapped broken string
See Also:
createWordWrappedString( String input, int fixedLength, String delimiter )

createWordWrappedString

public static String createWordWrappedString(String input,
                                             int fixedLength,
                                             String delimiter)
Take an input string S, break the string into parts of N up to F characters long which word wrapped with respective to blank space characters and separated with the supplied delimiter D.

Written by Peter Pilgrim Mon Mar 15 23:12:15 GMT 2004

Parameters:
input - the input string
fixedLength - the maximum fixed length
delimiter - the delimiter
Returns:
wrapped broken string

main

public static void main(String[] args)

unquote

public static String unquote(String s)
remove surrounding quote marks (either "" or ''), only if quotes are at both the beginning and end of the string

Parameters:
s - string for testing
Returns:
string which has surrounding quotes, if any, removed

startsAndEnds

public static boolean startsAndEnds(String s,
                                    String search)
determine whether a string both starts and ends with the given substring

Parameters:
s - string to look at
search - substring to search for
Returns:
true if string is surrounded by search string

Expresso 5-6

Please see www.jcorporate.com for information about new Expresso releases.