util
Class StringTool

java.lang.Object
  extended by util.StringTool

public class StringTool
extends Object

A package containing various static helper methods for dealing with Strings.


Nested Class Summary
static class StringTool.DoubleTest
          Test double-list parsing.
static class StringTool.Parse
          Test token-parsing.
static class StringTool.PropTest
          Test properties tokenization.
static class StringTool.Test
          Tests number parsing.
 
Field Summary
static String COLON
          A delimiter String containing additionally the colon.
static String COMMA
          A delimiter String containing additionally the comma.
private static String DELIM
          The default delimiter characters, as a string.
static String DOLLAR
          The dollar sign.
static String EQUAL
          A delimiter String containing additionally the equal sign.
static String INNERLIST
          The default delimiter for the inner list elements of a double list.
static String MAPDELIM
          The default delimiter between individual key-value mappings.
static String MAPKEY
          The default delimiter between key and values.
static String OUTERLIST
          The default delimiter for the outer list elements of a double list.
static String SEMICOLON
          A delimiter String containing additionally the semicolon.
static char UNDERSCORE
          The default replacement char for whitespaces.
 
Constructor Summary
StringTool()
           
 
Method Summary
static String cleanISO(String read)
          Removes any ISO-control chars from a string.
static String concat(String[] cl)
          Concatenates individual strings into a single string separated by os-specific newlines.
static String concat(String[] cl, String sep)
          Concatenates individual strings into a single string separated by os-specific newlines.
static String concat(String c1, String c2)
          Concatenates individual strings into a single string separated by os-specific newlines.
static String concat(String c1, String c2, String sep)
          Concatenates individual strings into a single string separated by os-specific newlines.
static int count(String aim, char find)
          Counts the occurence of the given char in the argument string.
static String discardBefore(String token, String all)
          If the token string is found in the argument, we return only the portion of the argument after the last occurence of the token, otherwise we return the entrire string
static List<List<String>> doubleTokenize(String target)
          Converts a string into a double-list.
static List<List<String>> doubleTokenize(String target, String outdelim, String indelim)
          Converts a string into a double list.
static List<List<String>> doubleTokenize(String target, String outdelim, String indelim, boolean trim)
          Converts a string into a double list.
static String dup(char what, int howoften)
          Returns a String containing howoften representations of char what.
private static
<T> T
getToken(String in, Class<T> def)
          Tries to convert a string into an object of the given class using a single argument constructor.
static String leftString(String arg, int left)
          Returns a String of length left, starting from arg index 0.
static Map<String,String> linkedMap(String target)
          Converts this string into a propertis object.
static Map<String,String> linkedMap(String target, String delim, String key)
          Converts this string into a propertis object.
static Map<String,String> linkedMap(String target, String delim, String key, boolean trim)
          Converts this string into a propertis object.
static String noWhitespace(String replace)
          Takes an input string an replaces all occurences of whitespaces with UNDERSCORE.
static String noWhitespace(String replace, char with)
          Takes an input string an replaces all occurences of whitespaces with the specified char.
static Number[] parseNumbers(String in, String[] names)
          Converts a string that consists of name/value pairs into numbers.
static Number[] parseNumbers(String in, String[] names, boolean ignorecase, Class[] types)
          Converts a string that consists of name/value pairs into numbers.
static Number[] parseNumbers(String in, String[] names, Class[] type)
          Converts a string that consists of name/value pairs into numbers.
static
<K,V> Map<K,V>
parseProperties(Map<String,String> target, Class<K> k, Class<V> v)
          Propertizes a mapping into keys and values of the stated reference.
static
<K,V> Map<K,V>
parseProperties(String target, Class<K> k, Class<V> v)
          Propertizes a mapping into keys and values of the stated reference.
static
<T> List<T>
parseToken(String tokens, Class<T> def)
          Converts a tokenizable list into a list of objects that can be constructed via a string.
static
<T> List<T>
parseToken(String tokens, Class<T> def, String delim)
          Converts a tokenizable list into a list of objects that can be constructed via a string.
static
<T> List<T>
parseToken(String tokens, Class<T> def, String delim, boolean trim, int length)
          Converts a tokenizable list into a list of objects that can be constructed via a string.
static Map<String,String> propertize(String target)
          Converts this string into a propertis object.
static Map<String,String> propertize(String target, String delim, String key)
          Converts this string into a propertis object.
static Map<String,String> propertize(String target, String delim, String key, boolean trim)
          Converts this string into a propertis object.
static String removeSpace(String arg)
          Removes all whitespaces from the argument string.
static String rightString(String arg, int right)
          Returns the last right characters of arg as a String.
static String stringList(List<?> l)
          Converts a list of strings into a single, comma-separated string.
static String stringMap(Map<?,?> m)
          Converts astring-tostring mapping into a single string, delimited with colons and equal signe.
static String stringNumbers(String[] names, Number[] values)
          Takes the array names, adds an equal sign after it and converts the number with the appropriate index into a string and adds it immediately after the equal sign.
static List<String> tokenize(String target)
          Converts this String of tokens into a vector of tokens using the default delimiters.
static List<String> tokenize(String target, String delim)
          Converts this String of tokens into a vector of tokens using the default delimiters.
static List<String> tokenize(String target, String delim, boolean trim)
          Converts this String of tokens into a vector of tokens using the specified delimiters.
static String toTokenString(Collection<?> stringlist, String delim)
          Concats a list of strings into a comma-separated list in a single string.
static String[] unchain(String all)
          Separates a single string into an array of individual lines.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DELIM

private static final String DELIM
The default delimiter characters, as a string.

See Also:
Constant Field Values

COMMA

public static final String COMMA
A delimiter String containing additionally the comma.

See Also:
Constant Field Values

SEMICOLON

public static final String SEMICOLON
A delimiter String containing additionally the semicolon.

See Also:
Constant Field Values

COLON

public static final String COLON
A delimiter String containing additionally the colon.

See Also:
Constant Field Values

EQUAL

public static final String EQUAL
A delimiter String containing additionally the equal sign.

See Also:
Constant Field Values

DOLLAR

public static final String DOLLAR
The dollar sign.

See Also:
Constant Field Values

MAPDELIM

public static final String MAPDELIM
The default delimiter between individual key-value mappings.

See Also:
Constant Field Values

MAPKEY

public static final String MAPKEY
The default delimiter between key and values.

See Also:
Constant Field Values

INNERLIST

public static final String INNERLIST
The default delimiter for the inner list elements of a double list.

See Also:
Constant Field Values

OUTERLIST

public static final String OUTERLIST
The default delimiter for the outer list elements of a double list.

See Also:
Constant Field Values

UNDERSCORE

public static final char UNDERSCORE
The default replacement char for whitespaces.

See Also:
Constant Field Values
Constructor Detail

StringTool

public StringTool()
Method Detail

stringList

public static String stringList(List<?> l)
Converts a list of strings into a single, comma-separated string.


stringMap

public static String stringMap(Map<?,?> m)
Converts astring-tostring mapping into a single string, delimited with colons and equal signe.


propertize

public static Map<String,String> propertize(String target)
Converts this string into a propertis object. Properties are used because the sole string-to-string mapping. This version uses the default delimiter MAPDELIM for separation between individual mappings, i.e. between key-value pairs. The delimiter separating the key from the value is MAPKEY. Both, the keys and the values, are trimmed prior to adding them to the Properties returned.

Parameters:
target - A string consisting of a MAPDELIM separated listing of individual key-value mapping, which are separated with the MAPKEY delimiter.
Returns:
A hash map, if order is needed use linkedMap(java.lang.String).

propertize

public static Map<String,String> propertize(String target,
                                            String delim,
                                            String key)
Converts this string into a propertis object. Properties are used because the sole string-to-string mapping. Both, the keys and the values, are trimmed prior to adding them to the Properties returned.

Parameters:
target - A string consisting of a delim separated listing of individual key-value mapping, which are separated with the key delimiter.
delim - A string used as a separator between individual key-value mappings.
key - The string used to separate the key from the value.
Returns:
A hash map, if order is needed use linkedMap(java.lang.String).

propertize

public static Map<String,String> propertize(String target,
                                            String delim,
                                            String key,
                                            boolean trim)
Converts this string into a propertis object. Properties are used because the sole string-to-string mapping.

Parameters:
target - A string consisting of a delim separated listing of individual key-value mapping, which are separated with the key delimiter.
delim - A string used as a separator between individual key-value mappings.
key - The string used to separate the key from the value. May not be a subset of delim.
trim - If true, both, the keys and values are trimmed prior to adding them to the properties object.
Returns:
A hash map, if order is needed use linkedMap(java.lang.String).

linkedMap

public static Map<String,String> linkedMap(String target)
Converts this string into a propertis object. Properties are used because the sole string-to-string mapping. This version uses the default delimiter MAPDELIM for separation between individual mappings, i.e. between key-value pairs. The delimiter separating the key from the value is MAPKEY. Both, the keys and the values, are trimmed prior to adding them to the Properties returned.

Parameters:
target - A string consisting of a MAPDELIM separated listing of individual key-value mapping, which are separated with the MAPKEY delimiter.
Returns:
A mapping maintaining the key order as in the string

linkedMap

public static Map<String,String> linkedMap(String target,
                                           String delim,
                                           String key)
Converts this string into a propertis object. Properties are used because the sole string-to-string mapping. Both, the keys and the values, are trimmed prior to adding them to the Properties returned.

Parameters:
target - A string consisting of a delim separated listing of individual key-value mapping, which are separated with the key delimiter.
delim - A string used as a separator between individual key-value mappings.
key - The string used to separate the key from the value.
Returns:
A mapping maintaining the key order as in the string

linkedMap

public static Map<String,String> linkedMap(String target,
                                           String delim,
                                           String key,
                                           boolean trim)
Converts this string into a propertis object. Properties are used because the sole string-to-string mapping.

Parameters:
target - A string consisting of a delim separated listing of individual key-value mapping, which are separated with the key delimiter.
delim - A string used as a separator between individual key-value mappings.
key - The string used to separate the key from the value. May not be a subset of delim.
trim - If true, both, the keys and values are trimmed prior to adding them to the properties object.
Returns:
A mapping maintaining the key order as in the string

tokenize

public static List<String> tokenize(String target)
Converts this String of tokens into a vector of tokens using the default delimiters.

Parameters:
target - The string to be tokenized.
Returns:
A vector of all tokens in the string.

tokenize

public static List<String> tokenize(String target,
                                    String delim)
Converts this String of tokens into a vector of tokens using the default delimiters.

Parameters:
target - The string to be tokenized.
Returns:
A vector of all tokens in the string.

tokenize

public static List<String> tokenize(String target,
                                    String delim,
                                    boolean trim)
Converts this String of tokens into a vector of tokens using the specified delimiters.

Parameters:
target - The string to be tokenized.
delim - The delimiters to use.
Returns:
A vector of all tokens in the string.

doubleTokenize

public static final List<List<String>> doubleTokenize(String target)
Converts a string into a double-list. This means, the list returned is a list of lists. These individual lists (called 'inner lists') consist of strings. The size of the inner lists may vary.
Using the default INNERLIST and OUTERLIST delimiters a string is parsed into a double list as in the example below:
 a,b,c,d;1,2;q,w,e,r,t,y;AA,BB,CC
 
This string will return a list with four elements (separator ';'). Each element is a list, the first one a four-string list (a, b, c, d), the second one a two-string list (1, 2), etc.


doubleTokenize

public static final List<List<String>> doubleTokenize(String target,
                                                      String outdelim,
                                                      String indelim)
Converts a string into a double list. This means, the list returned is a list of lists. These individual lists (called 'inner lists') consist of strings. The size of the inner lists may vary.
Using the default INNERLIST and OUTERLIST delimiters a string is parsed into a double list as in the example below:
 a,b,c,d;1,2;q,w,e,r,t,y;AA,BB,CC
 
This string will return a list with four elements (separator ';'). Each element is a list, the first one a four-string list (a, b, c, d), the second one a two-string list (1, 2), etc.


doubleTokenize

public static final List<List<String>> doubleTokenize(String target,
                                                      String outdelim,
                                                      String indelim,
                                                      boolean trim)
Converts a string into a double list. This means, the list returned is a list of lists. These individual lists (called 'inner lists') consist of strings. The size of the inner lists may vary.
Using the default INNERLIST and OUTERLIST delimiters a string is parsed into a double list as in the example below:
 a,b,c,d;1,2;q,w,e,r,t,y;AA,BB,CC
 
This string will return a list with four elements (separator ';'). Each element is a list, the first one a four-string list (a, b, c, d), the second one a two-string list (1, 2), etc.
Care must be taken in supplying the correct delimiter lists. It must be avoided to use the same delimiter in the inner and outer delimiter list. Only then, parsing can be successful.

Parameters:
target - The string to parse.
outdelim - The delimiter list of outer delimiters.
indelim - The delimiter list of inner delimiters.
trim - If true, the resulting strings are trimmed.

removeSpace

public static final String removeSpace(String arg)
Removes all whitespaces from the argument string. The cleared string is returned. Uses the same whitespace-definition as the StringTokenizer class.

Parameters:
arg - The input string
Returns:
A string where all whitespaces are removed

discardBefore

public static String discardBefore(String token,
                                   String all)
If the token string is found in the argument, we return only the portion of the argument after the last occurence of the token, otherwise we return the entrire string

Parameters:
token - The normally single-char token string
all - A long string that should be chopped for meaningful info.
Returns:
The trailing portion after token

dup

public static final String dup(char what,
                               int howoften)
Returns a String containing howoften representations of char what.


count

public static final int count(String aim,
                              char find)
Counts the occurence of the given char in the argument string.


leftString

public static final String leftString(String arg,
                                      int left)
Returns a String of length left, starting from arg index 0.


rightString

public static final String rightString(String arg,
                                       int right)
Returns the last right characters of arg as a String.


concat

public static final String concat(String c1,
                                  String c2)
Concatenates individual strings into a single string separated by os-specific newlines.


concat

public static final String concat(String c1,
                                  String c2,
                                  String sep)
Concatenates individual strings into a single string separated by os-specific newlines.


concat

public static final String concat(String[] cl)
Concatenates individual strings into a single string separated by os-specific newlines.


concat

public static final String concat(String[] cl,
                                  String sep)
Concatenates individual strings into a single string separated by os-specific newlines.


unchain

public static final String[] unchain(String all)
Separates a single string into an array of individual lines. The breakpoints are the os-specific newline strings.


toTokenString

public static final String toTokenString(Collection<?> stringlist,
                                         String delim)
Concats a list of strings into a comma-separated list in a single string.


parseToken

public static <T> List<T> parseToken(String tokens,
                                     Class<T> def)
                          throws ParseException
Converts a tokenizable list into a list of objects that can be constructed via a string. Missing tokens are replaced with the default values, until the required size is reached. The instance of the list object is inferred from the default value, thus this may not be null.

Parameters:
def - A default object, may not be null.
Throws:
ParseException

parseToken

public static <T> List<T> parseToken(String tokens,
                                     Class<T> def,
                                     String delim)
                          throws ParseException
Converts a tokenizable list into a list of objects that can be constructed via a string. Missing tokens are replaced with the default values, until the required size is reached. The instance of the list object is inferred from the default value, thus this may not be null.

Parameters:
def - A default object, may not be null.
Throws:
ParseException

parseToken

public static <T> List<T> parseToken(String tokens,
                                     Class<T> def,
                                     String delim,
                                     boolean trim,
                                     int length)
                          throws ParseException
Converts a tokenizable list into a list of objects that can be constructed via a string. Missing tokens are replaced with the default values, until the required size is reached. The instance of the list object is inferred from the default value, thus this may not be null.

Parameters:
def - A default object, may not be null.
Throws:
ParseException

parseProperties

public static <K,V> Map<K,V> parseProperties(String target,
                                             Class<K> k,
                                             Class<V> v)
                                throws ParseException
Propertizes a mapping into keys and values of the stated reference.

Throws:
ParseException

parseProperties

public static <K,V> Map<K,V> parseProperties(Map<String,String> target,
                                             Class<K> k,
                                             Class<V> v)
                                throws ParseException
Propertizes a mapping into keys and values of the stated reference.

Throws:
ParseException

getToken

private static <T> T getToken(String in,
                              Class<T> def)
                   throws ParseException
Tries to convert a string into an object of the given class using a single argument constructor.

Throws:
ParseException

parseNumbers

public static Number[] parseNumbers(String in,
                                    String[] names)
Converts a string that consists of name/value pairs into numbers. Variable names are not case sensitive. All values returned are scanned as doubles.

See Also:
parseNumbers(String, String[], boolean, Class[])

parseNumbers

public static Number[] parseNumbers(String in,
                                    String[] names,
                                    Class[] type)
Converts a string that consists of name/value pairs into numbers. Variable names are not case sensitive.

See Also:
parseNumbers(String, String[], boolean, Class[])

parseNumbers

public static Number[] parseNumbers(String in,
                                    String[] names,
                                    boolean ignorecase,
                                    Class[] types)
Converts a string that consists of name/value pairs into numbers. The format of the string must follow the format:
 [WS][WS,=][WS],
 
repeated for n-times. Additionally to the input string, the variable names must be provided at input. Depending on the ignorecase flag, this names are matched to the variable names in the input string. The number-class array provided on input is used for parsing the values to the appropriate type. If it is omitted, all values are parsed as doubles. The returned number array is an array consisting of number objects of the appropriate type and with values according to the input string. The ordering is according to the ordering of the variable names. Variables not found are null. Ordering of the variables within the string is unimportant. Note that at least one whitespace or equal char must separate the variable name from the value.

Parameters:
in - The input string.
names - The varaible names to extract out of the string.
ignorecase - If true, case of variable names is ignored.
types - A class array with the number types that should be parsed.
Returns:
An array of numbers, filled with the parsed values.

stringNumbers

public static String stringNumbers(String[] names,
                                   Number[] values)
Takes the array names, adds an equal sign after it and converts the number with the appropriate index into a string and adds it immediately after the equal sign. Adds a separator char after each name/value pair. Inverts parseNumbers(java.lang.String, java.lang.String[])


noWhitespace

public static String noWhitespace(String replace)
Takes an input string an replaces all occurences of whitespaces with UNDERSCORE.


noWhitespace

public static String noWhitespace(String replace,
                                  char with)
Takes an input string an replaces all occurences of whitespaces with the specified char.


cleanISO

public static String cleanISO(String read)
Removes any ISO-control chars from a string.