org.objectweb.telosys.util
Class StrUtil

java.lang.Object
  extended byorg.objectweb.telosys.util.StrUtil

public final class StrUtil
extends java.lang.Object

Utility class for String operations


Method Summary
static int countChar(java.lang.String s, char c)
          Count the number of occurrences of the given character in the given string
static boolean getBoolean(java.lang.String sVal)
          Converts the given string to boolean, without Exception.
static java.util.Date getDate(java.lang.String sDate)
          Returns the Date instance corresponding to the given date/time/datetime parameter in ISO format :
. "" or null : returns null
.
static java.sql.Date getDateFR(java.lang.String sDate)
          Return a date instance from a date in French format
static java.sql.Date getDateISO(java.lang.String sDate)
          Return a date instance from a date in ISO format
static double getDouble(java.lang.String sVal)
          Converts the given string to double, without Exception.
static double getDouble(java.lang.String sVal, double dDefaultValue)
          Converts the given string to double, without Exception.
static double getDoubleStrict(java.lang.String sVal)
          Converts the given string to double, throws an Exception if the value cannot be converted.
static int getInt(java.lang.String sVal)
          Converts the given string to integer, without Exception.
static int getInt(java.lang.String sVal, int iDefaultValue)
          Converts the given string to integer, without Exception.
static int getIntStrict(java.lang.String sVal)
          Converts the given string to integer, throws an Exception if the value cannot be converted.
static java.lang.String getLimitedString(java.lang.String sVal, int iMaxLength)
          Returns a truncated string if its length exceed the max parameter.
static java.lang.String getProtectedString(java.lang.String sVal)
          Replaces all the '"' characters by '"', returns "" if the given string is null
static boolean isFirstCharAlpha(java.lang.String s)
          Returns TRUE if the first character of the string is an alphabetic character
( from 'a' to 'z' and from 'A' to 'Z' )
static java.lang.String notNull(java.lang.String s)
          Returns a "not null" string
static boolean nullOrVoid(java.lang.String s)
          Returns true if the given String is null or void or blank ( "", " ", " " )
static java.lang.String removeAllBlanks(java.lang.String s)
          Remove all the blank characters in the given string
eg : returns "abc" for " a b c "
static java.lang.String removeQuotes(java.lang.String s, char c)
          Removes the quote characters if they are located at the first and last position of the string The string is returned "as is" if there's no quotes at the begining and at the end
static java.lang.String replaceVar(java.lang.String s, java.lang.String sVarName, java.lang.String sVarValue)
          Replace the given variable name by the given value
Only the first occurrence of the variable is replaced.
static java.lang.String[] split(java.lang.String s, char c)
          Split a string using the given char as separator ( simple split without "reg exp" )
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

countChar

public static int countChar(java.lang.String s,
                            char c)
Count the number of occurrences of the given character in the given string

Parameters:
s -
c -
Returns:
the number of char c in string s

getBoolean

public static boolean getBoolean(java.lang.String sVal)
Converts the given string to boolean, without Exception.

Parameters:
sVal - : the string to convert
Returns:
boolean : true is the string is "1" or "true" ( after trim ), else ( other values or null ) false

getLimitedString

public static java.lang.String getLimitedString(java.lang.String sVal,
                                                int iMaxLength)
Returns a truncated string if its length exceed the max parameter. Returns "" if the given string is null

Parameters:
sVal - :
iMaxLength - :
Returns:
String truncated string or same string if length <= max length

getProtectedString

public static java.lang.String getProtectedString(java.lang.String sVal)
Replaces all the '"' characters by '"', returns "" if the given string is null

Parameters:
sVal - :
Returns:
String string with protected characters

notNull

public static java.lang.String notNull(java.lang.String s)
Returns a "not null" string

Parameters:
s - :
Returns:
String the given string if not null, a void string ("") if null

nullOrVoid

public static boolean nullOrVoid(java.lang.String s)
Returns true if the given String is null or void or blank ( "", " ", " " )

Parameters:
s -
Returns:

getInt

public static int getInt(java.lang.String sVal,
                         int iDefaultValue)
Converts the given string to integer, without Exception. The default value is used if the given value is null or invalid.

Parameters:
sVal - value to convert
iDefaultValue - default value to use if the string is null or contains an invalid integer value
Returns:

getIntStrict

public static int getIntStrict(java.lang.String sVal)
Converts the given string to integer, throws an Exception if the value cannot be converted.

Parameters:
sVal -
Returns:
Throws:
TelosysRuntimeException - if the given value is null or invalid

getInt

public static int getInt(java.lang.String sVal)
Converts the given string to integer, without Exception. The default value is ZERO (used if the given value is null or invalid)

Parameters:
sVal -
Returns:
int

getDouble

public static double getDouble(java.lang.String sVal,
                               double dDefaultValue)
Converts the given string to double, without Exception. The default value is used if the given value is null or invalid.

Parameters:
sVal - value to convert
dDefaultValue - : Default value if the string is null or contains an invalid value
Returns:

getDoubleStrict

public static double getDoubleStrict(java.lang.String sVal)
Converts the given string to double, throws an Exception if the value cannot be converted.

Parameters:
sVal -
Returns:
Throws:
TelosysRuntimeException - if the given value is null or invalid

getDouble

public static double getDouble(java.lang.String sVal)
Converts the given string to double, without Exception. The default value is ZERO (used if the given value is null or invalid)

Parameters:
sVal - value to convert
Returns:
int :

getDate

public static java.util.Date getDate(java.lang.String sDate)
Returns the Date instance corresponding to the given date/time/datetime parameter in ISO format :
. "" or null : returns null
. "YYYY-MM-DD" : returns date
. "HH:MM:SS" : returns time
. "YYYY-MM-DD HH:MM:SS" : returns date and time

Parameters:
sDate -
Returns:
java.util.Date instance or null if the given String is null or void
Throws:
TelosysRuntimeException - if the given string cannot be converted
Since:
1.0.2

getDateFR

public static java.sql.Date getDateFR(java.lang.String sDate)
Return a date instance from a date in French format

Parameters:
sDate - : date in French format ( "DD/MM/YYYY" )
Returns:
java.sql.Date : the date instance ( or null is the input date is not valid )

getDateISO

public static java.sql.Date getDateISO(java.lang.String sDate)
Return a date instance from a date in ISO format

Parameters:
sDate - : date in ISO format ( "YYYY-MM-DD" )
Returns:
java.sql.Date : the date instance ( or null is the input date is not valid )

removeAllBlanks

public static final java.lang.String removeAllBlanks(java.lang.String s)
Remove all the blank characters in the given string
eg : returns "abc" for " a b c "

Parameters:
s -
Returns:

removeQuotes

public static java.lang.String removeQuotes(java.lang.String s,
                                            char c)
Removes the quote characters if they are located at the first and last position of the string The string is returned "as is" if there's no quotes at the begining and at the end

Parameters:
s - the string
c - the quote character to use ( eg '\'', '"' )
Returns:

split

public static java.lang.String[] split(java.lang.String s,
                                       char c)
Split a string using the given char as separator ( simple split without "reg exp" )

Parameters:
s - : the string to split
c - : the separator
Returns:
: array of 'tokens' ( never null, size = 0 if the string is null, else 1 to N )

isFirstCharAlpha

public static boolean isFirstCharAlpha(java.lang.String s)
Returns TRUE if the first character of the string is an alphabetic character
( from 'a' to 'z' and from 'A' to 'Z' )

Parameters:
s -
Returns:

replaceVar

public static java.lang.String replaceVar(java.lang.String s,
                                          java.lang.String sVarName,
                                          java.lang.String sVarValue)
Replace the given variable name by the given value
Only the first occurrence of the variable is replaced.

Parameters:
s - the initial string ( e.g. "bla bla bla ${MyVar} bla bla" )
sVarName - the variable name ( e.g. "${MyVar}" )
sVarValue - the variable value
Returns:
the string after variable replacement