Class StringUtils
- java.lang.Object
-
- org.esa.snap.core.util.StringUtils
-
public class StringUtils extends Object
TheStringUtils
class provides frequently used utility methods dealing withString
values and which are not found in thejava.lang.String
class.All functions have been implemented with extreme caution in order to provide a maximum performance.
- Version:
- $Revision$ $Date$
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StringUtils.CaseType
-
Field Summary
Fields Modifier and Type Field Description static String
DELIMITOR_SPACE
static String
DELIMITOR_UNDERSCORE
-
Constructor Summary
Constructors Modifier Constructor Description protected
StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String[]
addArrays(String[] arr1, String[] arr2)
Returns a string array which is a concatenation of the two given string arrays.static String[]
addToArray(String[] array, String toAdd)
Gives a new StringArray who contains both, all Strings form the given Array and the given String.static boolean
areEntriesUnique(String[] array)
Retrieves whether the entries in the string array are unique - or not.static String
arrayToCsv(Object array)
Gets a comma separate value string for the given array object.static String
arrayToString(Object array, String s)
Converts an array into a string.static boolean
contains(String[] a, String s)
Tests whether a given string is contained in a given string array.static boolean
containsIgnoreCase(String[] a, String s)
Tests whether a given string is contained in a given string array.static boolean
containsIgnoreCase(String input, String value)
static boolean
containsIgnoreCase(List<String> l, String s)
Tests whether a given string is contained in a list.static String
createValidName(String name, char[] validChars, char replaceChar)
Creates a valid name for the given source name.static String[]
csvToArray(String csvString)
Gets a String[] from the given comma separated value string.static boolean
endsWithIgnoreCase(String input, String... suffixes)
Tests if this string ends with any of the given suffixes, ignoring the case sensitive.static String
firstLetterUp(String string)
Turns the first letter of the given string to upper case.static String
formatColor(Color c)
Returns a string representation of the given color value.static String
getStringCaseVariation(String s, StringUtils.CaseType caseType)
static String[]
getStringCaseVariations(String s)
static int
indexOf(String[] a, String s)
Gets the array index of a given string in a given string array or-1
if the string could not be found.static int
indexOfIgnoreCase(String[] a, String s)
Gets the array index of a given string in a given string array or-1
if the string could not be found.static int
indexOfIgnoreCase(List<String> l, String s)
Gets the list index of a given string in a given string list or-1
if the string could not be found.static int
indexOfSpecificOccurrence(String a, String s, int i)
Gets the array index of the i-th occurrence of a given string in a string array or-1
if the string could not be found.static boolean
isIdentifier(String s)
Tests whether or not the given string is valid identifier.static boolean
isIntegerString(String token)
Checks whether the given token string represents an integer number or not.static boolean
isIntegerString(String token, int radix)
Checks whether the given token string represents an integer number or not.static boolean
isNotNullAndNotEmpty(String str)
Tests whether or not the given string is not null and not empty.static boolean
isNullOrBlank(String value)
Tests if the input string is null, or if it contains only white spaces.static boolean
isNullOrEmpty(String str)
Tests whether or not the given string is null or empty.static boolean
isNumeric(String str, Class<? extends Number> clazz)
Checks if the string is numericstatic String
join(Object[] tokens, String separator)
Joins the given array of tokens to a new text string.static String
join(List<? extends Object> tokens, String separator)
Joins the given array of tokens to a new text string.static String[]
makeStringsUnique(String[] strings)
Makes the strings in the given array unique.static String
padNum(int num, int max, char c)
Adds padding to an integer 1 becomes 001 or __1static Color
parseColor(String text)
Converts a textual representation of an RGB(A) color to aColor
object.static String[]
removeFromArray(String[] array, String toRemove)
Gives a new StringArray who contains all Strings form the given Array excepting the given String.static String[]
removeFromArray(String[] array, String[] toRemove)
Gives a new StringArray who contains all Strings form the given Array excepting the String from the array to remove.static String
replaceWord(String string, String oldWord, String newWord)
Replaces all occurrences of the old word within the given string by the new word.static String[]
split(String text, char[] separators, boolean trimTokens)
Splits the given text into a list of tokens by using the supplied separators.static List<String>
split(String text, char[] separators, boolean trimTokens, List<String> tokens)
Splits the given text into a list of tokens by using the supplied separators.static boolean
startsWithIgnoreCase(String inputValue, String prefix)
Tests if this string starts with the specified prefix, ignoring the case sensitive.static String[]
stringToArray(String csvString, String delim)
Gets a String[] from the given comma separated value string given a delimiter.static double[]
toDoubleArray(String text, String delim)
Converts the given text into andouble
array.static float[]
toFloatArray(String text, String delim)
Converts the given text into anfloat
array.static int[]
toIntArray(String text, String delim)
Converts the given text into anint
array.static String[]
toStringArray(Object[] objArray)
Converts the given object array into a string array.static String[]
toStringArray(String text, String delims)
Converts the given text into an array ofString
tokens.static String
toXMLString(org.jdom2.Document element)
static String
toXMLString(org.jdom2.Element element)
static String
toXMLString(org.jdom2.output.Format format, org.jdom2.Document element)
static String
toXMLString(org.jdom2.output.Format format, org.jdom2.Element element)
-
-
-
Method Detail
-
split
public static List<String> split(String text, char[] separators, boolean trimTokens, List<String> tokens)
Splits the given text into a list of tokens by using the supplied separators. Empty tokens are created for successive separators, or if the supplied text starts with or ends with a separator. If the given text string is empty, and empty list is returned, but nevernull
. The tokens added to list will never contain separators.- Parameters:
text
- the text to be split into tokensseparators
- the characters used to separate the tokenstrimTokens
- if true, white space characters are removed from both ends of each tokentokens
- can be null. If not null, all tokens are added to this list and the method it, otherwise a new list is created.- Returns:
- a list of tokens extracted from the given text, never
null
- Throws:
IllegalArgumentException
- if one of the arguments was null- See Also:
StringTokenizer
-
split
public static String[] split(String text, char[] separators, boolean trimTokens)
Splits the given text into a list of tokens by using the supplied separators. Empty tokens are created for successive separators, or if the the supplied text starts with or ends with a separator. If the given text string is empty, and empty array is returned, but nevernull
. The tokens in the returned array will never contain separators.- Parameters:
text
- the text to be splitted into tokensseparators
- the characters used to separate the tokenstrimTokens
- if true, white space characters are removed from both ends of each token- Returns:
- an array of tokens extracted from the given text, never
null
- See Also:
StringTokenizer
-
join
public static String join(Object[] tokens, String separator)
Joins the given array of tokens to a new text string. The given separator string is put between each of the tokens. If a token in the array isnull
or empty, an empty string string is appended to the resulting text. The resulting text string will always containtokens.length - 1
separators, if the separator is not part of one of the tokens itself.- Parameters:
tokens
- the list of tokens to join, must not be nullseparator
- the separator string, must not be null- Returns:
- the list of tokens as a text string
- Throws:
IllegalArgumentException
- if one of the arguments was null- See Also:
split(String, char[], boolean)
-
join
public static String join(List<? extends Object> tokens, String separator)
Joins the given array of tokens to a new text string. The given separator string is put between each of the tokens. If a token in the array isnull
or empty, an empty string is appended to the resulting text. The resulting text string will always containtokens.length - 1
separators, if the separator is not part of one of the tokens itself.- Parameters:
tokens
- the list of tokens to join, must not be nullseparator
- the separator string, must not be null- Returns:
- the list of tokens as a text string
- Throws:
IllegalArgumentException
- if one of the arguments was null- See Also:
join(Object[], String)
-
isIntegerString
public static boolean isIntegerString(String token)
Checks whether the given token string represents an integer number or not.- Parameters:
token
- the token string to be checked- Returns:
true
if the string represents an integer (radix=10)
-
isIntegerString
public static boolean isIntegerString(String token, int radix)
Checks whether the given token string represents an integer number or not.- Parameters:
token
- the token string to be checkedradix
- the radix of the integer represented by the token string- Returns:
true
if the string represents an integer with tzhe given radix
-
toIntArray
public static int[] toIntArray(String text, String delim)
Converts the given text into anint
array.The number values are expected to be separated by one of the characters given in the delimiter string.
If the delimiter string is null or empty, the default delimiter "," will be used.
- Parameters:
text
- the text to be converteddelim
- the delimiter between the number values- Returns:
- the
int
array parsed from the given text - Throws:
IllegalArgumentException
- if the text is null or cannot be converted to an array of the requested number type
-
toFloatArray
public static float[] toFloatArray(String text, String delim)
Converts the given text into anfloat
array.The number values are expected to be separated by one of the characters given in the delimiter string.
If the delimiter string is null or empty, the default delimiter "," will be used.
- Parameters:
text
- the text to be converteddelim
- the delimiter between the number values- Returns:
- the
float
array parsed from the given text - Throws:
IllegalArgumentException
- if the text is null or cannot be converted to an array of the requested number type
-
toDoubleArray
public static double[] toDoubleArray(String text, String delim)
Converts the given text into andouble
array.The number values are expected to be separated by one of the characters given in the delimiter string.
If the delimiter string is null or empty, the default delimiter "," will be used.
- Parameters:
text
- the text to be converteddelim
- the delimiter between the number values- Returns:
- the
double
array parsed from the given text - Throws:
IllegalArgumentException
- if the text is null or cannot be converted to an array of the requested number type
-
toStringArray
public static String[] toStringArray(String text, String delims)
Converts the given text into an array ofString
tokens.The number values are expected to be separated by one of the characters given in the delimiter string.
If the delimiter string is null or empty, the default delimiter "," will be used.
- Parameters:
text
- the text to be converteddelims
- the delimiter characters used between the tokens- Returns:
- the
String
array parsed from the given text, never null - Throws:
IllegalArgumentException
- if the text is null or cannot be converted to an array of the requested number type
-
toStringArray
public static String[] toStringArray(Object[] objArray)
Converts the given object array into a string array. If the given object array is already an instance of a string array, it is simply type-casted and returned. Otherwise, a new string array is created and - for each non-null object - the value ofobject.toString()
is stored, null-values remain null-values.- Parameters:
objArray
- the object array to be converted, ifnull
the method returnsnull
too- Returns:
- the string array
-
isNullOrEmpty
public static boolean isNullOrEmpty(String str)
Tests whether or not the given string is null or empty.- Parameters:
str
- the string to be tested- Returns:
true
if so
-
isNotNullAndNotEmpty
public static boolean isNotNullAndNotEmpty(String str)
Tests whether or not the given string is not null and not empty.- Parameters:
str
- the string to be tested- Returns:
true
if so
-
isNullOrBlank
public static boolean isNullOrBlank(String value)
Tests if the input string is null, or if it contains only white spaces.- Parameters:
value
- the input string to check- Returns:
true
if the input string is null or blank;false
otherwise.
-
addToArray
public static String[] addToArray(String[] array, String toAdd) throws IllegalArgumentException
Gives a new StringArray who contains both, all Strings form the given Array and the given String. The given String was added to the end of array- Returns:
- new
String[]
with all Strings - Throws:
IllegalArgumentException
- if one of the arguments arenull
-
removeFromArray
public static String[] removeFromArray(String[] array, String toRemove) throws IllegalArgumentException
Gives a new StringArray who contains all Strings form the given Array excepting the given String. The first occurrence of the given String was removed.- Returns:
- new
String[]
without the first occurrence of the given String - Throws:
IllegalArgumentException
- if one of the arguments arenull
-
removeFromArray
public static String[] removeFromArray(String[] array, String[] toRemove) throws IllegalArgumentException
Gives a new StringArray who contains all Strings form the given Array excepting the String from the array to remove. The first occurrence of the given Strings in the string array to remove was removed.- Returns:
- new
String[]
without the first occurrence of the given Strings in the string array to remove - Throws:
IllegalArgumentException
- if array isnull
-
addArrays
public static String[] addArrays(String[] arr1, String[] arr2) throws IllegalArgumentException
Returns a string array which is a concatenation of the two given string arrays.- Returns:
- a new
String[]
which is a concatenation of the two given string arrays. - Throws:
IllegalArgumentException
- if one of the arguments arenull
-
contains
public static boolean contains(String[] a, String s)
Tests whether a given string is contained in a given string array.- Parameters:
a
- the string array in which to searchs
- the string for which the search is performed- Returns:
true
if the strings
is contained in the arraya
- Throws:
IllegalArgumentException
- if one of the arguments arenull
-
containsIgnoreCase
public static boolean containsIgnoreCase(String[] a, String s)
Tests whether a given string is contained in a given string array.- Parameters:
a
- the string array in which to searchs
- the string for which the search is performed- Returns:
true
if the strings
is contained in the arraya
- Throws:
IllegalArgumentException
- if one of the arguments arenull
-
startsWithIgnoreCase
public static boolean startsWithIgnoreCase(String inputValue, String prefix)
Tests if this string starts with the specified prefix, ignoring the case sensitive.- Parameters:
inputValue
- the input string to testprefix
- the prefix- Returns:
true
if the input string is a prefix;false
otherwise.
-
endsWithIgnoreCase
public static boolean endsWithIgnoreCase(String input, String... suffixes)
Tests if this string ends with any of the given suffixes, ignoring the case sensitive.- Parameters:
input
- the input string to testsuffixes
- the list of suffixes- Returns:
true
if the input string is a prefix;false
otherwise.
-
containsIgnoreCase
public static boolean containsIgnoreCase(List<String> l, String s)
Tests whether a given string is contained in a list.- Parameters:
l
- the string list in which to searchs
- the string for which the search is performed- Returns:
true
if the strings
is contained in the arraya
- Throws:
IllegalArgumentException
- if one of the arguments arenull
-
areEntriesUnique
public static boolean areEntriesUnique(String[] array)
Retrieves whether the entries in the string array are unique - or not.- Parameters:
array
- the array containing the strings- Returns:
true
if the entries in the string array are unique, otherwisefalse
.
-
indexOf
public static int indexOf(String[] a, String s)
Gets the array index of a given string in a given string array or-1
if the string could not be found.- Parameters:
a
- the string array in which to searchs
- the string for which the search is performed- Returns:
- the array index of the first occurence of
s
ina
or-1
if it is not cointained in the array - Throws:
IllegalArgumentException
- if one of the arguments isnull
-
indexOfIgnoreCase
public static int indexOfIgnoreCase(String[] a, String s)
Gets the array index of a given string in a given string array or-1
if the string could not be found.- Parameters:
a
- the string array in which to searchs
- the string for which the search is performed- Returns:
- the array index of the first occurrence of
s
ina
or-1
if it is not contained in the array - Throws:
IllegalArgumentException
- if one of the arguments isnull
-
indexOfIgnoreCase
public static int indexOfIgnoreCase(List<String> l, String s)
Gets the list index of a given string in a given string list or-1
if the string could not be found.- Parameters:
l
- the string list in which to searchs
- the string for which the search is performed- Returns:
- the array index of the first occurrence of
s
ina
or-1
if it is not contained in the array - Throws:
IllegalArgumentException
- if one of the arguments isnull
-
arrayToCsv
public static String arrayToCsv(Object array)
Gets a comma separate value string for the given array object.- Parameters:
array
- the array object of values.- Returns:
- a comma separate value string for the given array object.
- Throws:
IllegalArgumentException
- if the given Object is not anarray
ornull
.
-
arrayToString
public static String arrayToString(Object array, String s)
Converts an array into a string.- Parameters:
array
- the array objects
- the separator string, e.g. ","- Returns:
- a string represenation of the array
- Throws:
IllegalArgumentException
- if the given Object is not anarray
ornull
.
-
csvToArray
public static String[] csvToArray(String csvString)
Gets a String[] from the given comma separated value string.- Parameters:
csvString
- the CSV (comma separated value) String.- Returns:
- an array of strings created from the given comma separated value string, never
null
- Throws:
IllegalArgumentException
- if the given csvString isnull
orempty
.
-
stringToArray
public static String[] stringToArray(String csvString, String delim)
Gets a String[] from the given comma separated value string given a delimiter.- Parameters:
csvString
- the delimited String.delim
- the separator string, e.g. ","- Returns:
- an array of strings created from the given comma separated value string, never
null
- Throws:
IllegalArgumentException
- if the given Object is not anarray
ornull
.
-
parseColor
public static Color parseColor(String text) throws NumberFormatException
Converts a textual representation of an RGB(A) color to aColor
object.- Throws:
NumberFormatException
-
formatColor
public static String formatColor(Color c)
Returns a string representation of the given color value.
-
createValidName
public static String createValidName(String name, char[] validChars, char replaceChar)
Creates a valid name for the given source name. The method returns a string which is the given name where each occurence of a character which is not a letter, a digit or one of the given valid characters is replaced by the given replace character. The returned string always has the same length as the source name.- Parameters:
name
- the source name, must not benull
validChars
- the array of valid charactersreplaceChar
- the replace character
-
replaceWord
public static String replaceWord(String string, String oldWord, String newWord)
Replaces all occurrences of the old word within the given string by the new word.- Parameters:
string
- the string within all occurrences of the old word are to be replaced.oldWord
- the word to be replaced. Must not benull
and must not be empty.newWord
- the new word. Must not benull
.- Returns:
- the resulting string, never
null
.
-
isIdentifier
public static boolean isIdentifier(String s)
Tests whether or not the given string is valid identifier. Valid identifiers have a length greater than zero, start with a letter or underscore followed by letters, digits or underscores.- Parameters:
s
- the string to test- Returns:
true
if the s is a valid node ifentifier,false
otherwise
-
isNumeric
public static boolean isNumeric(String str, Class<? extends Number> clazz)
Checks if the string is numeric- Parameters:
str
- the String inputclazz
- the type to check against- Returns:
- true if numeric false if not
-
firstLetterUp
public static String firstLetterUp(String string)
Turns the first letter of the given string to upper case.- Parameters:
string
- the string to change- Returns:
- a changed string
-
indexOfSpecificOccurrence
public static int indexOfSpecificOccurrence(String a, String s, int i)
Gets the array index of the i-th occurrence of a given string in a string array or-1
if the string could not be found.- Parameters:
a
- the string array in which to searchs
- the string for which the search is performedi
- the index of the occurrence of the requested string- Returns:
- the array index of the
i-th
occurrence ofs
ina
or-1
ifs
is contained less thani
times in the array or ifi
is less than 1 - Throws:
IllegalArgumentException
- ifa
s
arenull
-
padNum
public static String padNum(int num, int max, char c)
Adds padding to an integer 1 becomes 001 or __1- Parameters:
num
- the integer valuemax
- the desired string lengthc
- the inserted character- Returns:
- padded number as string
-
makeStringsUnique
public static String[] makeStringsUnique(String[] strings)
Makes the strings in the given array unique. Strings which occur more than once in the array get an appendix with the index of its occurrence. Strings that only differ in their capitalization will be treated as having the same string value, but in the output they will keep their respective capitalization Example:'name', 'duplicate', 'other', 'duplicate', 'case_duplicate', 'CASE_DUPLICATE' --> 'name', 'duplicate_1', 'other', 'duplicate_2', 'case_duplicate_1', 'CASE_DUPLICATE_2'
- Parameters:
strings
- the strings to make unique- Returns:
- a new array with the changed names
-
toXMLString
public static String toXMLString(org.jdom2.Element element)
-
toXMLString
public static String toXMLString(org.jdom2.output.Format format, org.jdom2.Element element)
-
toXMLString
public static String toXMLString(org.jdom2.Document element)
-
toXMLString
public static String toXMLString(org.jdom2.output.Format format, org.jdom2.Document element)
-
getStringCaseVariation
public static String getStringCaseVariation(String s, StringUtils.CaseType caseType)
-
-