public class StringUtils extends Object
StringUtils
class provides frequently used utility methods dealing with String
values
and which are not found in the java.lang.String
class.
All functions have been implemented with extreme caution in order to provide a maximum performance.
Modifier | Constructor and Description |
---|---|
protected |
StringUtils() |
Modifier and Type | Method and 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 or not a given string is contained in a given string array.
|
static boolean |
containsIgnoreCase(List l,
String s)
Tests whether or not a given string is contained in a list.
|
static boolean |
containsIgnoreCase(String[] a,
String s)
Tests whether or not a given string is contained in a given string array.
|
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 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 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(List 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 |
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 |
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 |
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 numeric
|
static String |
join(List tokens,
String separator)
Joins the given array of tokens to a new text string.
|
static String |
join(Object[] tokens,
String separator)
Joins the given array of tokens to a new text string.
|
static String |
padNum(int num,
int max,
char c)
Adds padding to an integer
1 becomes 001 or __1
|
static Color |
parseColor(String text)
Converts a textual representation of an RGB(A) color to a
Color 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 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 an
double array. |
static float[] |
toFloatArray(String text,
String delim)
Converts the given text into an
float array. |
static int[] |
toIntArray(String text,
String delim)
Converts the given text into an
int 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 of
String tokens. |
public static List<String> split(String text, char[] separators, boolean trimTokens, List<String> tokens)
null
. The tokens added to list will never contain
separators.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 tokentokens
- can be null. If not null, all tokens are added to this list and the method it, otherwise a new
list is created.null
IllegalArgumentException
- if one of the arguments was nullStringTokenizer
public static String[] split(String text, char[] separators, boolean trimTokens)
null
. The tokens in the returned array will never
contain separators.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 tokennull
StringTokenizer
public static String join(Object[] tokens, String separator)
null
or empty, an empty string string is appended to the
resulting text. The resulting text string will always contain tokens.length - 1
separators, if the
separator is not part of one of the tokens itself.tokens
- the list of tokens to join, must not be nullseparator
- the separator string, must not be nullIllegalArgumentException
- if one of the arguments was nullsplit(String, char[], boolean)
public static String join(List tokens, String separator)
null
or empty, an empty string string is appended to the
resulting text. The resulting text string will always contain tokens.length - 1
separators, if the
separator is not part of one of the tokens itself.tokens
- the list of tokens to join, must not be nullseparator
- the separator string, must not be nullIllegalArgumentException
- if one of the arguments was nulljoin(Object[], String)
public static boolean isIntegerString(String token)
token
- the token string to be checkedtrue
if the string represents an integer (radix=10)public static boolean isIntegerString(String token, int radix)
token
- the token string to be checkedradix
- the radix of the integer represented by the token stringtrue
if the string represents an integer with tzhe given radixpublic static int[] toIntArray(String text, String delim)
int
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.
text
- the text to be converteddelim
- the delimiter between the number valuesint
array parsed from the given textIllegalArgumentException
- if the text is null or cannot be converted to an array of the requested number
typepublic static float[] toFloatArray(String text, String delim)
float
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.
text
- the text to be converteddelim
- the delimiter between the number valuesfloat
array parsed from the given textIllegalArgumentException
- if the text is null or cannot be converted to an array of the requested number
typepublic static double[] toDoubleArray(String text, String delim)
double
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.
text
- the text to be converteddelim
- the delimiter between the number valuesdouble
array parsed from the given textIllegalArgumentException
- if the text is null or cannot be converted to an array of the requested number
typepublic static String[] toStringArray(String text, String delims)
String
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.
text
- the text to be converteddelims
- the delimiter characters used between the tokensString
array parsed from the given text, never nullIllegalArgumentException
- if the text is null or cannot be converted to an array of the requested number
typepublic static String[] toStringArray(Object[] objArray)
object.toString()
is stored, null-values remain null-values.objArray
- the object array to be converted, if null
the method returns null
toopublic static boolean isNullOrEmpty(String str)
str
- the string to be testedtrue
if sopublic static boolean isNotNullAndNotEmpty(String str)
str
- the string to be testedtrue
if sopublic static String[] addToArray(String[] array, String toAdd) throws IllegalArgumentException
String[]
with all StringsIllegalArgumentException
- if one of the arguments are null
public static String[] removeFromArray(String[] array, String toRemove) throws IllegalArgumentException
String[]
without the first occurrence of the given StringIllegalArgumentException
- if one of the arguments are null
public static String[] removeFromArray(String[] array, String[] toRemove) throws IllegalArgumentException
String[]
without the first occurrence of the given Strings in the string array to
removeIllegalArgumentException
- if array is null
public static String[] addArrays(String[] arr1, String[] arr2) throws IllegalArgumentException
String[]
which is a concatenation of the two given string arrays.IllegalArgumentException
- if one of the arguments are null
public static boolean contains(String[] a, String s)
a
- the string array in which to searchs
- the string for which the search is performedtrue
if the string s
is contained in the array a
IllegalArgumentException
- if one of the arguments are null
public static boolean containsIgnoreCase(String[] a, String s)
a
- the string array in which to searchs
- the string for which the search is performedtrue
if the string s
is contained in the array a
IllegalArgumentException
- if one of the arguments are null
public static boolean containsIgnoreCase(List l, String s)
l
- the string list in which to searchs
- the string for which the search is performedtrue
if the string s
is contained in the array a
IllegalArgumentException
- if one of the arguments are null
public static boolean areEntriesUnique(String[] array)
array
- true
if the entries in the string array are unique, otherwise false
.public static int indexOf(String[] a, String s)
-1
if the string could not be
found.a
- the string array in which to searchs
- the string for which the search is performeds
in a
or -1
if it is
not cointained in the arrayIllegalArgumentException
- if one of the arguments is null
public static int indexOfIgnoreCase(String[] a, String s)
-1
if the string could not be
found.a
- the string array in which to searchs
- the string for which the search is performeds
in a
or -1
if it is
not cointained in the arrayIllegalArgumentException
- if one of the arguments is null
public static int indexOfIgnoreCase(List l, String s)
-1
if the string could not be
found.l
- the string list in which to searchs
- the string for which the search is performeds
in a
or -1
if it is
not cointained in the arrayIllegalArgumentException
- if one of the arguments is null
public static String arrayToCsv(Object array)
array
- the array object of values.IllegalArgumentException
- if the given Object is not an array
or null
.public static String arrayToString(Object array, String s)
array
- the array objects
- the separator string, e.g. ","IllegalArgumentException
- if the given Object is not an array
or null
.public static String[] csvToArray(String csvString)
csvString
- the CSV (comma separated value) String.null
IllegalArgumentException
- if the given csvString is null
or empty
.public static String[] stringToArray(String csvString, String delim)
csvString
- the delimited String.delim
- the separator string, e.g. ","null
IllegalArgumentException
- if the given Object is not an array
or null
.public static Color parseColor(String text) throws NumberFormatException
Color
object.NumberFormatException
public static String formatColor(Color c)
public static String createValidName(String name, char[] validChars, char replaceChar)
name
- the source name, must not be null
validChars
- the array of valid charactersreplaceChar
- the replace characterpublic static String replaceWord(String string, String oldWord, String newWord)
string
- the string within all occurrences of the old word are to be replaced.oldWord
- the word to be replaced. Must not be null
and must not be empty.newWord
- the new word. Must not be null
.null
.public static boolean isIdentifier(String s)
s
- the string to testtrue
if the s is a valid node ifentifier, false
otherwisepublic static boolean isNumeric(String str, Class<? extends Number> clazz)
str
- the String inputclazz
- the type to check againstpublic static String firstLetterUp(String string)
string
- the string to changepublic static int indexOfSpecificOccurrence(String a, String s, int i)
-1
if
the string could not be found.a
- the string array in which to searchs
- the string for which the search is performedi
- the index of the occurrence of the requested stringi-th
occurrence of s
in a
or
-1
if s
is contained less than i
times in the array or if
i
is less than 1IllegalArgumentException
- if a
s
are null
public static String padNum(int num, int max, char c)
num
- the integer valuemax
- the desired string lengthc
- the inserted characterCopyright © 2014–2017 European Space Agency (ESA). All rights reserved.