org.apache.empire.commons
Class StringUtils

java.lang.Object
  extended by org.apache.empire.commons.StringUtils

public class StringUtils
extends Object

This class contains common functions for comparing and converting values of type String.


Method Summary
static String arrayToString(Object[] array, String separator)
          Converts an array of objects to a string.
static String coalesce(String preferred, String alternative)
          Returns the preferred String if it is not empty ot the alternative String otherwise.
static boolean compareEqual(String s1, String s2, boolean ignoreCase)
          Compares two Strings with each other - either with or without character case.
static boolean isCharBetween(char c, char beg, char end)
          returns true if the character c is between the characters beg and end
static boolean isEmpty(String s)
          Checks if a string is empty
static boolean isLower(char c)
          returns true if the character c is an upper case character ('a'-'z')
static boolean isLower(String s, int count)
          returns true if the first count characters of s are all lower case (or other non-case sensitive characters)
static boolean isNotEmpty(String s)
          Checks if a string is not null or empty
static boolean isNumber(char c)
          returns true if the character c is a number digit ('0'-'9')
static boolean isNumber(String s)
          returns true if the string s is a number (contains only the characters 0 to 9)
static boolean isNumeric(char c, boolean decimal)
          returns true if the character c is a numeric digit ('+' || '-' || '.' || ',' || '0'-'9')
static boolean isNumeric(String s, boolean decimal)
          returns true if the string s contains only numeric digits ('+' || '-' || '.' || ',' || '0'-'9')
static boolean isUpper(char c)
          returns true if the character c is an upper case character ('A'-'Z')
static boolean isUpper(String s, int count)
          returns true if the first count characters of s are all upper case (or other non-case sensitive characters)
static boolean isValid(String s)
          Deprecated. this has been renamed to isNotEmpty
static String nullIfEmpty(Object value)
          Returns null if the value supplied is null or an empty String.
static String remove(String s, char c)
          Removes all occurrences of c from s
static String remove(String s, String remove)
          Removes all occurrences of remove from s
static String removeBlanks(String s)
          removes all blanks from s
static String replace(String source, String find, String replace)
          Replaces all occurrences of find in source by replace.
static String replaceAll(String s, String find, String replace)
          Returns a String with all occurrences of from within orig replaced with to.
static String toLower(String s, int count)
          makes the first n characters of s lower case
static String toString(Object value)
          Converts a value to a string.
static String toString(Object[] array)
          Converts an array of objects to a string.
static String toString(Object[] array, String defValue)
          Converts an array of objects to a string.
static String toString(Object value, String defValue)
          Converts a value to a string.
static String toUpper(String s, int count)
          makes the first n characters of s upper case
static String validate(String s)
          Validates a given string.
static String valueOf(Object value)
          Converts a value to a string.
static String valueOf(Object[] array)
          Converts an objects to a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toString

public static String toString(Object value,
                              String defValue)
Converts a value to a string. If the value is null then the default value is returned.

Parameters:
value - the value to convert
defValue - default value which to return if value is null
Returns:
returns a String representation of the value or null if value is null

toString

public static String toString(Object value)
Converts a value to a string. If the value is null then null will be returned.

Parameters:
value - the value to convert
Returns:
returns a String representation of the value or null if value is null

toString

public static String toString(Object[] array,
                              String defValue)
Converts an array of objects to a string.

Parameters:
array - array of objects
defValue - default value which to return if array is null
Returns:
returns a String representation of the array or the defaultValue if array is null

toString

public static String toString(Object[] array)
Converts an array of objects to a string.

Parameters:
array - array of objects
Returns:
returns a String representation of the array or null if the array is null

valueOf

public static String valueOf(Object value)
Converts a value to a string. if the value is null an empty string is returned.

Parameters:
value - the value to convert
Returns:
returns a String representation of the Object or an empty stringif o is null

valueOf

public static String valueOf(Object[] array)
Converts an objects to a string.

Parameters:
array - array of objects
Returns:
returns a String representation of the array or an empty String if the array is null

coalesce

public static String coalesce(String preferred,
                              String alternative)
Returns the preferred String if it is not empty ot the alternative String otherwise.

Parameters:
preferred - the preferred String
alternative - the alternative String if the preferred String is not valid
Returns:
the preferred String if it is not empty ot the alternative String otherwise

nullIfEmpty

public static String nullIfEmpty(Object value)
Returns null if the value supplied is null or an empty String.

Parameters:
value - the value to check
Returns:
null if the value supplied is null or an empty String or the value as a string otherwise

arrayToString

public static String arrayToString(Object[] array,
                                   String separator)
Converts an array of objects to a string.

Parameters:
array - array of objects
separator - the separator to put between the object strings
Returns:
returns a String

isEmpty

public static boolean isEmpty(String s)
Checks if a string is empty

Parameters:
s - the String to check
Returns:
true if s is empty or null

isValid

@Deprecated
public static boolean isValid(String s)
Deprecated. this has been renamed to isNotEmpty

Checks if a string is not null or empty

Parameters:
s - the string to validate
Returns:
true if valid

isNotEmpty

public static boolean isNotEmpty(String s)
Checks if a string is not null or empty

Parameters:
s - the string to validate
Returns:
true if valid

compareEqual

public static boolean compareEqual(String s1,
                                   String s2,
                                   boolean ignoreCase)
Compares two Strings with each other - either with or without character case. Both arguments may be null.

Parameters:
s1 - the first String
s2 - the second String
ignoreCase - whether to ignore the character casing or not
Returns:
true if the two strings supplied are equal

validate

public static String validate(String s)
Validates a given string. If the string is empty then null is returned. Otherwise the trimmed string is returned.

Parameters:
s - the string to validate
Returns:
the string or null if s was empty.

replace

public static String replace(String source,
                             String find,
                             String replace)
Replaces all occurrences of find in source by replace.

Parameters:
source - the original String.
find - the String to be replaced
replace - the replacement string
Returns:
a new string with all occurrences of find in source replaced by replace

replaceAll

public static String replaceAll(String s,
                                String find,
                                String replace)
Returns a String with all occurrences of from within orig replaced with to. If orig contains no occurrences of from, or if from is equal to to,orig itself is returned rather than a copy being made. If orig is null, null is returned.

Parameters:
s - the original String.
find - the String to be replaced
replace - the replacement string
Returns:
a new string with all occurrences of find in source replaced by replace

remove

public static String remove(String s,
                            String remove)
Removes all occurrences of remove from s

Parameters:
s - the source string
remove - the string to remove
Returns:
the result string

remove

public static String remove(String s,
                            char c)
Removes all occurrences of c from s

Parameters:
s - the source string
c - the character to remove
Returns:
the result string

removeBlanks

public static String removeBlanks(String s)
removes all blanks from s

Parameters:
s - the source string
Returns:
the result string

isCharBetween

public static boolean isCharBetween(char c,
                                    char beg,
                                    char end)
returns true if the character c is between the characters beg and end

Parameters:
c - the source character
beg - the lower end character
end - the higher end character
Returns:
true if the c is between beg and end, or false otherwise

isNumber

public static boolean isNumber(char c)
returns true if the character c is a number digit ('0'-'9')

Parameters:
c - the source character
Returns:
true if the c is between 0 and 9

isNumber

public static boolean isNumber(String s)
returns true if the string s is a number (contains only the characters 0 to 9)

Parameters:
s - the source string
Returns:
true if s contains only the characters 0 to 9

isNumeric

public static boolean isNumeric(char c,
                                boolean decimal)
returns true if the character c is a numeric digit ('+' || '-' || '.' || ',' || '0'-'9')

Parameters:
c - the source character
decimal - flag to indicate whether the decimal and grouping separators ('.' || ',') are allowed
Returns:
true if the c is a valid numeric character

isNumeric

public static boolean isNumeric(String s,
                                boolean decimal)
returns true if the string s contains only numeric digits ('+' || '-' || '.' || ',' || '0'-'9')

Parameters:
s - the source string
decimal - flag to indicate whether the decimal and grouping separators ('.' || ',') are allowed
Returns:
true if s contains only numeric digits

isUpper

public static boolean isUpper(char c)
returns true if the character c is an upper case character ('A'-'Z')

Parameters:
c - the character
Returns:
true if c is an upper case character

isUpper

public static boolean isUpper(String s,
                              int count)
returns true if the first count characters of s are all upper case (or other non-case sensitive characters)

Parameters:
s - the source string
Returns:
true if the first count characters of s are all upper case

isLower

public static boolean isLower(char c)
returns true if the character c is an upper case character ('a'-'z')

Parameters:
c - the character
Returns:
true if c is an upper case character

isLower

public static boolean isLower(String s,
                              int count)
returns true if the first count characters of s are all lower case (or other non-case sensitive characters)

Parameters:
s - the source string
Returns:
true if the first count characters of s are all lower case

toUpper

public static String toUpper(String s,
                             int count)
makes the first n characters of s upper case

Parameters:
s - the source string
count - the number of characters to convert
Returns:
the result string

toLower

public static String toLower(String s,
                             int count)
makes the first n characters of s lower case

Parameters:
s - the source string
count - the number of characters to convert
Returns:
the result string


Copyright © 2008–2014 Apache Software Foundation. All rights reserved.