Package org.apache.empire.commons
Class StringUtils
java.lang.Object
org.apache.empire.commons.StringUtils
This class contains common functions for comparing and converting values of type String.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classObjectStringifier Converts an object to a String -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringarrayToString(Object[] array) Converts an array of objects to a string.static StringarrayToString(Object[] array, String separator) Converts an array of objects to a string.static StringarrayToString(Object[] array, String template, String defItemValue) Converts an array of objects to a string.static StringarrayToString(Object[] array, String template, String defItemValue, StringUtils.ObjectStringifier stringifier) Converts an array of objects to a string.static StringConverts a value to a string.static StringReturns the preferred String if it is not empty ot the alternative String otherwise.static booleancompareEqual(String s1, String s2) Compares two Strings with each other - either with or without character case.static booleancompareEqual(String s1, String s2, boolean ignoreCase) Compares two Strings with each other - either with or without character case.static booleancompareNotEqual(String s1, String s2) Compares two Strings with each other - either with or without character case.static booleancompareNotEqual(String s1, String s2, boolean ignoreCase) Compares two Strings with each other - either with or without character case.static StringAssembles a string from several partsstatic StringconcatItems(char separator, Object... params) Assembles a string from parts with a separator charstatic booleanReturns true if the given substring is part of the string provided by valuestatic intestimateArrayBufferSize(Object[] array, StringUtils.ObjectStringifier stringifier, int separatorLength, int defValueLength, boolean ignoreEmpty) Estimates the buffer size needed to convert an Array into a Stringstatic intestimateListBufferSize(Collection<?> list, StringUtils.ObjectStringifier stringifier, int separatorLength, int defValueLength, boolean ignoreEmpty) Estimates the buffer size needed to convert a Collection into a Stringstatic intindexOfAny(String value, char... chars) Returns the first index of any of the given characters in value starting from the beginningstatic booleanisCharBetween(char c, char beg, char end) returns true if the character c is between the characters beg and endstatic booleanChecks if a string is emptystatic booleanisLower(char c) returns true if the character c is an upper case character ('a'-'z')static booleanreturns true if the first count characters of s are all lower case (or other non-case sensitive characters)static booleanisNotEmpty(String s) Checks if a string is not null or emptystatic booleanisNumber(char c) returns true if the character c is a number digit ('0'-'9')static booleanreturns true if the string s is a number (contains only the characters 0 to 9)static booleanisNumeric(char c, boolean decimal) returns true if the character c is a numeric digit ('+' || '-' || '.' || ',' || '0'-'9')static booleanreturns true if the string s contains only numeric digits ('+' || '-' || '.' || ',' || '0'-'9')static booleanisUpper(char c) returns true if the character c is an upper case character ('A'-'Z')static booleanreturns true if the first count characters of s are all upper case (or other non-case sensitive characters)static intlastIndexOfAny(String value, char... chars) Returns the first index of any of the given characters in value starting from the endstatic intReturns the length of a Stringstatic StringlistToString(Collection<?> list) Converts a list (Collection) of objects to a string.static StringlistToString(Collection<?> list, String separator) Converts a list (Collection) of objects to a string.static StringlistToString(Collection<?> list, String template, String defItemValue) Converts a list (Collection) of objects to a string.static StringlistToString(Collection<?> list, String template, String defItemValue, StringUtils.ObjectStringifier stringifier) Converts a list (Collection) of objects to a string.static booleannotContains(String value, String substring) Returns true if the given substring is part of the string provided by valuestatic StringnullIfEmpty(Object value) Returns null if the value supplied is null or an empty String.static StringPads a String to the leftstatic StringPads a String to the rightstatic StringRemoves all occurrences of c from sstatic StringRemoves all occurrences of remove from sstatic Stringremoves all blanks from sstatic StringReplaces all occurrences of find in source by replace.static StringreplaceAll(String s, String find, String replace) Returns a String with all occurrences offromwithinorigreplaced withto.setObjectStringifier(StringUtils.ObjectStringifier stringifier) static StringtoCamelCase(String text, boolean firstCharUpper) Converts a String to camel case Words must be separated by underscorestatic Stringmakes the first n characters of s lower casestatic StringConverts a value to a string.static StringConverts a value to a string.static Stringmakes the first n characters of s upper casestatic Stringtruncates a string to a maximum number of charsstatic StringValidates a given string.static StringConverts a value to a string.
-
Field Details
-
EMPTY
Empty String- See Also:
-
SPACE
Single Space- See Also:
-
NULL
Null String- See Also:
-
DEFAULT_ITEM_SEPARATOR
Default Item Separator -
LIST_TEMPLATE
Default List Template -
TEMPLATE_SEP_CHAR
public static char TEMPLATE_SEP_CHAR
-
-
Method Details
-
getObjectStringifier
-
setObjectStringifier
public static StringUtils.ObjectStringifier setObjectStringifier(StringUtils.ObjectStringifier stringifier) -
toString
Converts a value to a string. If the value is null then the default value is returned.- Parameters:
value- the value to convertdefValue- default value which to return if value is null- Returns:
- returns a string for the object or the defValue if null
-
toString
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 for the object or null
-
valueOf
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 for the object or an empty string if null
-
asString
Converts a value to a string. Almost same as toString() but Lists and Arrays are wrapped with the standard list template (LIST_TEMPLATE) if the value is null an empty string is returned.- Parameters:
value- the value to convert- Returns:
- returns a string for the object or an empty string if null
-
length
Returns the length of a String- Parameters:
value- the string value- Returns:
- length of the string value or 0
-
coalesce
Returns the preferred String if it is not empty ot the alternative String otherwise.- Parameters:
preferred- the preferred Stringalternative- 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
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
-
contains
Returns true if the given substring is part of the string provided by value- Parameters:
value- the value to checksubstring- the substring- Returns:
- true if the given substring is part of the string provided by value
-
notContains
Returns true if the given substring is part of the string provided by value- Parameters:
value- the value to checksubstring- the substring- Returns:
- true if the given substring is part of the string provided by value
-
indexOfAny
Returns the first index of any of the given characters in value starting from the beginning- Parameters:
value- the value to checkchars- the characters to search- Returns:
- the index
-
lastIndexOfAny
Returns the first index of any of the given characters in value starting from the end- Parameters:
value- the value to checkchars- the characters to search- Returns:
- the index
-
arrayToString
public static String arrayToString(Object[] array, String template, String defItemValue, StringUtils.ObjectStringifier stringifier) Converts an array of objects to a string.- Parameters:
array- array of objectstemplate- the list template or item separatordefItemValue- the default item valuestringifier- interface to convert an object to a string- Returns:
- returns a String or null if the array is null or empty
-
arrayToString
Converts an array of objects to a string.- Parameters:
array- array of objectstemplate- the list template or item separatordefItemValue- the default item value- Returns:
- returns a String or null if the array is null or empty
-
arrayToString
Converts an array of objects to a string.- Parameters:
array- array of objectsseparator- the separator to put between the object strings- Returns:
- returns a String
-
arrayToString
Converts an array of objects to a string.- Parameters:
array- array of objects- Returns:
- returns a String
-
estimateArrayBufferSize
public static int estimateArrayBufferSize(Object[] array, StringUtils.ObjectStringifier stringifier, int separatorLength, int defValueLength, boolean ignoreEmpty) Estimates the buffer size needed to convert an Array into a String- Parameters:
array- the arraystringifier- interface to convert an object to a stringseparatorLength- the separator lengthdefValueLength- the default lengthignoreEmpty- flag whether to ignore empty parts- Returns:
- the estimated length of the collection parts
-
listToString
public static String listToString(Collection<?> list, String template, String defItemValue, StringUtils.ObjectStringifier stringifier) Converts a list (Collection) of objects to a string.- Parameters:
list- the collection of objectstemplate- the list template or item separatordefItemValue- the default item valuestringifier- interface to convert an object to a string- Returns:
- returns a String or null if the list is null
-
listToString
Converts a list (Collection) of objects to a string.- Parameters:
list- the collection of objectstemplate- the list template or item separatordefItemValue- the default item value- Returns:
- returns a String
-
listToString
Converts a list (Collection) of objects to a string.- Parameters:
list- the collection of objectsseparator- the separator to put between the object strings- Returns:
- returns a String
-
listToString
Converts a list (Collection) of objects to a string.- Parameters:
list- the collection of objects- Returns:
- returns a String
-
estimateListBufferSize
public static int estimateListBufferSize(Collection<?> list, StringUtils.ObjectStringifier stringifier, int separatorLength, int defValueLength, boolean ignoreEmpty) Estimates the buffer size needed to convert a Collection into a String- Parameters:
list- the list to estimatestringifier- interface to convert an object to a stringseparatorLength- the separator lengthdefValueLength- the default lengthignoreEmpty- flag whether to ignore empty parts- Returns:
- the estimated length of the collection parts
-
concatItems
Assembles a string from parts with a separator char- Parameters:
separator- the separator to put between the object stringsparams- array of objects- Returns:
- returns a String
-
concat
Assembles a string from several parts- Parameters:
parts- the parts to concatenate- Returns:
- returns a String
-
isEmpty
Checks if a string is empty- Parameters:
s- the String to check- Returns:
- true if s is empty or
null
-
isNotEmpty
Checks if a string is not null or empty- Parameters:
s- the string to validate- Returns:
- true if valid
-
compareEqual
Compares two Strings with each other - either with or without character case. Both arguments may be null.- Parameters:
s1- the first Strings2- the second StringignoreCase- whether to ignore the character casing or not- Returns:
- true if the two strings supplied are equal
-
compareEqual
Compares two Strings with each other - either with or without character case. Both arguments may be null.- Parameters:
s1- the first Strings2- the second String- Returns:
- true if the two strings supplied are equal
-
compareNotEqual
Compares two Strings with each other - either with or without character case. Both arguments may be null.- Parameters:
s1- the first Strings2- the second StringignoreCase- whether to ignore the character casing or not- Returns:
- true if the two strings supplied are not equal
-
compareNotEqual
Compares two Strings with each other - either with or without character case. Both arguments may be null.- Parameters:
s1- the first Strings2- the second String- Returns:
- true if the two strings supplied are not equal
-
validate
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
Replaces all occurrences of find in source by replace.- Parameters:
source- the original String.find- the String to be replacedreplace- the replacement string- Returns:
- a new string with all occurrences of
findinsourcereplaced byreplace
-
replaceAll
Returns a String with all occurrences offromwithinorigreplaced withto. Iforigcontains no occurrences offrom, or iffromis equal toto,origitself is returned rather than a copy being made. If orig isnull,nullis returned.- Parameters:
s- the original String.find- the String to be replacedreplace- the replacement string- Returns:
- a new string with all occurrences of
findinsourcereplaced byreplace
-
remove
Removes all occurrences of remove from s- Parameters:
s- the source stringremove- the string to remove- Returns:
- the result string
-
remove
Removes all occurrences of c from s- Parameters:
s- the source stringc- the character to remove- Returns:
- the result string
-
removeBlanks
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 characterbeg- the lower end characterend- 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
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 characterdecimal- flag to indicate whether the decimal and grouping separators ('.' || ',') are allowed- Returns:
- true if the c is a valid numeric character
-
isNumeric
returns true if the string s contains only numeric digits ('+' || '-' || '.' || ',' || '0'-'9')- Parameters:
s- the source stringdecimal- 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
returns true if the first count characters of s are all upper case (or other non-case sensitive characters)- Parameters:
s- the source stringcount- number of characters to check- 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
returns true if the first count characters of s are all lower case (or other non-case sensitive characters)- Parameters:
s- the source stringcount- number of characters to check- Returns:
- true if the first count characters of s are all lower case
-
toUpper
makes the first n characters of s upper case- Parameters:
s- the source stringcount- the number of characters to convert- Returns:
- the result string
-
toLower
makes the first n characters of s lower case- Parameters:
s- the source stringcount- the number of characters to convert- Returns:
- the result string
-
truncate
truncates a string to a maximum number of chars- Parameters:
s- the source stringmaxChar- the maximum number of chars- Returns:
- the result string
-
padLeft
Pads a String to the left- Parameters:
s- the string to padsize- the desired sizepadChar- the padding char- Returns:
- the padded string
-
padRight
Pads a String to the right- Parameters:
s- the string to padsize- the desired sizepadChar- the padding char- Returns:
- the padded string
-
toCamelCase
Converts a String to camel case Words must be separated by underscore- Parameters:
text- the string to convertfirstCharUpper- flag wether the first character should be upper case (true) or lower case (false)- Returns:
- the camel case string
-