org.esa.beam.util
Class StringUtils

java.lang.Object
  extended by org.esa.beam.util.StringUtils

public class StringUtils
extends Object

The 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.

Version:
$Revision$ $Date$
Author:
Tom Block, Norman Fomferra, Sabine Embacher

Constructor Summary
protected StringUtils()
           
 
Method Summary
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 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 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 split(String text, char[] separators, boolean trimTokens, List tokens)
          Splits the given text into a list of tokens by using the supplied separators.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtils

protected StringUtils()
Method Detail

split

public static List split(String text,
                         char[] separators,
                         boolean trimTokens,
                         List 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 the supplied text starts with or ends with a separator. If the given text string is empty, and empty list is returned, but never null. The tokens added to list will never contain separators.

Parameters:
text - the text to be splitted into tokens
separators - the characters used to separate the tokens
trimTokens - if true, white space characters are removed from both ends of each token
tokens - 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 never null. The tokens in the returned array will never contain separators.

Parameters:
text - the text to be splitted into tokens
separators - the characters used to separate the tokens
trimTokens - 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 is 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.

Parameters:
tokens - the list of tokens to join, must not be null
separator - 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 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 is 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.

Parameters:
tokens - the list of tokens to join, must not be null
separator - 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 checked
radix - 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 an 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.

Parameters:
text - the text to be converted
delim - 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 an 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.

Parameters:
text - the text to be converted
delim - 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 an 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.

Parameters:
text - the text to be converted
delim - 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 of 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.

Parameters:
text - the text to be converted
delims - 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 of object.toString() is stored, null-values remain null-values.

Parameters:
objArray - the object array to be converted, if null the method returns null 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

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 are null

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 are null

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 is null

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 are null

contains

public static boolean contains(String[] a,
                               String s)
Tests whether or not a given string is contained in a given string array.

Parameters:
a - the string array in which to search
s - the string for which the search is performed
Returns:
true if the string s is contained in the array a
Throws:
IllegalArgumentException - if one of the arguments are null

containsIgnoreCase

public static boolean containsIgnoreCase(String[] a,
                                         String s)
Tests whether or not a given string is contained in a given string array.

Parameters:
a - the string array in which to search
s - the string for which the search is performed
Returns:
true if the string s is contained in the array a
Throws:
IllegalArgumentException - if one of the arguments are null

containsIgnoreCase

public static boolean containsIgnoreCase(List l,
                                         String s)
Tests whether or not a given string is contained in a list.

Parameters:
l - the string list in which to search
s - the string for which the search is performed
Returns:
true if the string s is contained in the array a
Throws:
IllegalArgumentException - if one of the arguments are null

areEntriesUnique

public static boolean areEntriesUnique(String[] array)
Retrieves whether the entries in the string array are unique - or not.

Parameters:
array -
Returns:
true if the entries in the string array are unique, otherwise false.

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 search
s - the string for which the search is performed
Returns:
the array index of the first occurence of s in a or -1 if it is not cointained in the array
Throws:
IllegalArgumentException - if one of the arguments is null

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 search
s - the string for which the search is performed
Returns:
the array index of the first occurence of s in a or -1 if it is not cointained in the array
Throws:
IllegalArgumentException - if one of the arguments is null

indexOfIgnoreCase

public 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.

Parameters:
l - the string list in which to search
s - the string for which the search is performed
Returns:
the array index of the first occurence of s in a or -1 if it is not cointained in the array
Throws:
IllegalArgumentException - if one of the arguments is null

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 an array or null.

arrayToString

public static String arrayToString(Object array,
                                   String s)
Converts an array into a string.

Parameters:
array - the array object
s - the separator string, e.g. ","
Returns:
a string represenation of the array
Throws:
IllegalArgumentException - if the given Object is not an array or null.

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 is null or empty.

parseColor

public static Color parseColor(String text)
                        throws NumberFormatException
Converts a textual representation of an RGB(A) color to a Color 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 be null
validChars - the array of valid characters
replaceChar - 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 be null and must not be empty.
newWord - the new word. Must not be null.
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 input
clazz - 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


Copyright © 2002-2013 Brockmann Consult GmbH. All Rights Reserved.