spiffy.core.lang
Class StringHelper

java.lang.Object
  extended by spiffy.core.lang.StringHelper

public class StringHelper
extends Object

Helper methods for string manipulations and/or string representations of various entities

Author:
Kasper B. Graversen, (c) 2007

Constructor Summary
StringHelper()
           
 
Method Summary
static boolean in(String searchString, String... searchStringList)
          Given a string check to see if it is in a collection of strings (using equals())
static boolean inAndNonEmpty(String searchString, String... searchStringList)
          Given a string, trim it, and if different from the string "", check to see if it is in a collection of strings (using equals()).
static String join(String delimiter, Iterator<?> iterator)
          Join 0..n strings separated by a delimiter.
static String join(String delimiter, String... strings)
          Join 0..n strings separated by a delimiter.
static String removeAll(String baseString, Character... removeChars)
          Remove all occurrences of all specified characters.
static String repeatLeftJoin(String baseString, int endSize, String... joinStrings)
          Create a string by repeatedly joining/appending one or more strings in front of the string until the resulting string is equal to or as close possible to some length.
static String repeatRightJoin(String baseString, int endSize, String... joinStrings)
          Create a string by repeatedly joining/appending one or more strings at the end of the string until the resulting string is equal to or as close possible to some length.
static String toString(Throwable throwable)
          Returns a string representation of any throwable (e.g. exceptions)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringHelper

public StringHelper()
Method Detail

in

public static boolean in(String searchString,
                         String... searchStringList)
Given a string check to see if it is in a collection of strings (using equals())

Parameters:
searchString - string to search for. If null false is returned.
searchStringList - list of strings to search within
Returns:
true if string is in the search list, or false if searchString is null or not present in the list
Throws:
IllegalArgumentException - when argument SearchStringList has length > 0
Since:
0.03

inAndNonEmpty

public static boolean inAndNonEmpty(String searchString,
                                    String... searchStringList)
Given a string, trim it, and if different from the string "", check to see if it is in a collection of strings (using equals()).

This is quite often used in web programming, where input on the server side may either be null, "" or some string.

Parameters:
searchString - string to search for
searchStringList - list of strings to search within
Returns:
true if string is in the search list, or false if searchString is null or not present in the list
Throws:
IllegalArgumentException - when argument SearchStringList has length > 0
Since:
0.03
See Also:
in(String, String[])

join

public static String join(String delimiter,
                          Iterator<?> iterator)
Join 0..n strings separated by a delimiter. The delimiter is not concatenated after the last element. e.g. given an iterator with the elements ", ", "a", "b", "c"
 join(iterator)
 
becomes
 "a, b, c"
 

Parameters:
delimiter - the delimiter to insert between each string.
iterator - the iterator of elements to concatenate.
Returns:
the delimiter-concatenated string. If null is given as input strings, null is returned.

join

public static String join(String delimiter,
                          String... strings)
Join 0..n strings separated by a delimiter. The delimiter is not concatenated after the last element. e.g.
 join(", ", "a", "b", "c")
 
becomes
 "a, b, c"
 

Parameters:
delimiter - the delimiter to insert between each string.
strings - the array of strings to concatenate.
Returns:
the delimiter-concatenated string. If null is given as input strings, null is returned.

removeAll

public static String removeAll(String baseString,
                               Character... removeChars)
Remove all occurrences of all specified characters.

This is an easy way to e.g. remove all formatting chars such as ' ', '\t', '\n' from Strings for easy comparison of code generated String.

Parameters:
baseString - the string to trim
removeChars - the characters to remove
Returns:
a string with the specified characters removed.

repeatLeftJoin

public static String repeatLeftJoin(String baseString,
                                    int endSize,
                                    String... joinStrings)
Create a string by repeatedly joining/appending one or more strings in front of the string until the resulting string is equal to or as close possible to some length. E.g. * repeatLeftJoin("100", 6, "#")); yields "fully fill", "###100" repeatLeftJoin("100", 6, "##")); Yields "##100",

Parameters:
baseString - the string to join strings onto
endSize - the resulting size
joinStrings - one or more strings to join onto the basestring. If the empty list is given, the baseString is returned.
Returns:
a joined string

repeatRightJoin

public static String repeatRightJoin(String baseString,
                                     int endSize,
                                     String... joinStrings)
Create a string by repeatedly joining/appending one or more strings at the end of the string until the resulting string is equal to or as close possible to some length. E.g.
 repeatLeftJoin("100", 6, "#"));
 
Yields "fully fill", "###100"
 repeatLeftJoin("100", 6, "##"));
 
Yields "##100",

Parameters:
baseString - the string to join strings onto
endSize - the resulting size
joinStrings - one or more strings to join onto the basestring. If the empty list is given, the baseString is returned.
Returns:
a joined string

toString

public static String toString(Throwable throwable)
Returns a string representation of any throwable (e.g. exceptions)

Parameters:
throwable - the throwable/exception to get a representation from
Returns:
a string representing the throwable
Since:
0.1