Structured Text Toolkit

com.saelist.util
Class Strings

java.lang.Object
  extended bycom.saelist.util.Strings

public class Strings
extends java.lang.Object

Various text oriented utilites


Field Summary
static Logger logger
          A logger for this class.
 
Constructor Summary
Strings()
           
 
Method Summary
static java.lang.String arrayToString(java.lang.String[] values, char separator)
          Converts String array to a string with delimiter.
static java.lang.String asStackTrace(java.lang.Throwable e)
          Returns the stacktrace of the given exception as a string.
static boolean containsAnyOf(java.lang.String text, java.lang.String chars)
          Tests if the text contains any of the characters in chars .
static java.lang.String cutDown(java.lang.String text, int maxSize, java.lang.String suffix)
          Limits the size of text to maxSize charaters and appends the suffix.
static java.lang.String format(java.lang.String text, java.lang.String arg)
          A conveniance interface to format(String, String[]) for a single argument.
static java.lang.String format(java.lang.String text, java.lang.String[] args)
          Replaces each occurence of "%" in the text with the eachth element of args.
static java.lang.String format(java.lang.String text, java.lang.String arg1, java.lang.String arg2)
          A conveniance interface to format(String, String[]) for 2 arguments.
static java.lang.String format(java.lang.String text, java.lang.String arg1, java.lang.String arg2, java.lang.String arg3)
          A conveniance interface to format(String, String[]) for 3 arguments.
static java.lang.String format(java.lang.String text, java.lang.String arg1, java.lang.String arg2, java.lang.String arg3, java.lang.String arg4)
          A conveniance interface to format(String, String[]) for 4 arguments.
static java.lang.String front(java.lang.String text, java.lang.String sep)
          Returns the part of text before sep.
static java.lang.String getStackTrace(java.lang.Throwable t)
          Returns the stack-trace of a Throwable as String.
static java.lang.String indent(java.lang.String text, int n)
          Indents a string by n characters.
static java.lang.String interpret(java.lang.String text, java.util.Map substitutions)
          Primitive variable substitution for strings.
static java.lang.String join(java.util.List list, java.lang.String separator)
          Joins a List [a, b, c] into a string "a|b|c".
static java.lang.String join(java.lang.Object[] objects, java.lang.String delimiter)
          See join(List, String).
static java.lang.String lastFront(java.lang.String text, java.lang.String sep)
          Returns the part of text before the last sep.
static java.lang.String lastRest(java.lang.String text, java.lang.String sep)
          Returns the rest of text after last sep.
static java.lang.String loadFile(java.lang.String filename)
          Load the contents of a file as a string.
static java.lang.String loadReader(java.io.Reader reader)
          Loads the content of reader into a String.
static java.lang.String loadResource(java.lang.String name)
          Load a resource (file) on the classpath as a string.
static java.lang.String loadResource(java.lang.String name, java.lang.Class clazz)
          Load a resource (file) on the classpath as a string.
static java.lang.String loadStream(java.io.InputStream in)
          Load all content from the given stream as string.
static java.util.List makeList()
          A convenience method to make an empty list.
static java.util.List makeList(java.lang.Object arg1)
          A convenience method to make a one element list.
static java.util.List makeList(java.lang.Object[] args)
          A convenience method to enlist an array.
static java.util.List makeList(java.lang.Object arg1, java.lang.Object arg2)
          A convenience method to make a two element list.
static java.util.List makeList(java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)
          A convenience method to make a three element list.
static java.util.List makeList(java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4)
          A convenience method to make a four element list.
static java.util.Map makeMap(java.lang.Object key1, java.lang.Object value1)
          A convenience method to make a one entry map.
static java.util.Map makeMap(java.lang.Object key1, java.lang.Object value1, java.lang.Object key2, java.lang.Object value2)
          A convenience method to make a two entry map.
static java.util.Map makeMap(java.lang.Object key1, java.lang.Object value1, java.lang.Object key2, java.lang.Object value2, java.lang.Object key3, java.lang.Object value3)
          A convenience method to make a three entry map.
static java.util.Map makeMap(java.lang.Object key1, java.lang.Object value1, java.lang.Object key2, java.lang.Object value2, java.lang.Object key3, java.lang.Object value3, java.lang.Object key4, java.lang.Object value4)
          A convenience method to make a four entry map.
static boolean matches(java.lang.String text, java.lang.String pattern)
          Indicates wether the pattern matches the text.
static java.lang.String removeNull(java.lang.String value)
          Returns the empty string if value is null or "null".
static java.lang.String repeat(java.lang.String s, int n)
          Repeat the given string n times.
static java.lang.String replace(java.lang.String text, java.lang.String pattern, java.lang.String replacement)
          Substitutes all occurances of a pattern in a text replacementwith replacement.
static java.lang.String replace(java.lang.String text, java.lang.String pattern, java.lang.String replacement, int count)
          Substitutes the first count occurances of a pattern in a text with replacement.
static java.lang.String rest(java.lang.String text, java.lang.String sep)
          Returns the rest of text after first sep.
static void saveFile(java.lang.String filename, java.lang.String content)
           
static java.util.List split(java.lang.String text, java.lang.String separator)
          Splits a string "a|b|c" into list with elements ["a", "b", "c"] .
static java.lang.String substring(java.lang.String text, int start)
          Tolerant substring.
static java.lang.String substring(java.lang.String text, int start, int stop)
          Tolerant substring.
static java.lang.String[] tokenize(java.lang.String text, java.lang.String delimiters, boolean returndelimiters)
          Splits the text on the delimiters and returns an array of the strings.
static java.util.List toList(java.util.Enumeration e)
          Enlists an enumeration.
static java.lang.String toNonNullString(java.lang.Object object)
          Converts null strings to "", all others objects to their Object.toString().
static java.lang.String[] toStringArray(java.util.List list)
          Converts a List of Objects into a array of strings.
static void verify(boolean condition, java.lang.String message)
          Throws an exception with the given message if the condition is true.
static java.util.List wrap(java.lang.String prefix, java.util.List list, java.lang.String suffix)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

public static Logger logger
A logger for this class.

Constructor Detail

Strings

public Strings()
Method Detail

asStackTrace

public static java.lang.String asStackTrace(java.lang.Throwable e)
Returns the stacktrace of the given exception as a string.

Parameters:
e - the exception to trace.
Returns:
the stacktrace of the exception as a string.

containsAnyOf

public static boolean containsAnyOf(java.lang.String text,
                                    java.lang.String chars)
Tests if the text contains any of the characters in chars .

Parameters:
text - the string to search.
chars - the chars to find.
Returns:
weather text conains any of the characters.

cutDown

public static java.lang.String cutDown(java.lang.String text,
                                       int maxSize,
                                       java.lang.String suffix)
Limits the size of text to maxSize charaters and appends the suffix.

Parameters:
text - the text to constrain.
maxSize - the desired maximum length of text.
suffix - will be appended to the result if it is actaully cut down.
Returns:
The cut-down string with the suffix appended if it exceeded length. otherwise the original text.
Throws:
java.lang.NullPointerException - is text is null.

format

public static java.lang.String format(java.lang.String text,
                                      java.lang.String arg)
A conveniance interface to format(String, String[]) for a single argument.


format

public static java.lang.String format(java.lang.String text,
                                      java.lang.String arg1,
                                      java.lang.String arg2)
A conveniance interface to format(String, String[]) for 2 arguments.


format

public static java.lang.String format(java.lang.String text,
                                      java.lang.String arg1,
                                      java.lang.String arg2,
                                      java.lang.String arg3)
A conveniance interface to format(String, String[]) for 3 arguments.


format

public static java.lang.String format(java.lang.String text,
                                      java.lang.String arg1,
                                      java.lang.String arg2,
                                      java.lang.String arg3,
                                      java.lang.String arg4)
A conveniance interface to format(String, String[]) for 4 arguments.


format

public static java.lang.String format(java.lang.String text,
                                      java.lang.String[] args)
Replaces each occurence of "%" in the text with the eachth element of args. The process stops when it runs out of either.

Parameters:
text - the string to format.
args - the replacements for "%" in text.
Returns:
the formatted string

indent

public static java.lang.String indent(java.lang.String text,
                                      int n)
Indents a string by n characters. Inserts n spaces at the front of the string and after all occurences of newline. Un-optimized.

Parameters:
text - the string to indent
n - the number of spaces to indent with.
Returns:
the indented string.

interpret

public static java.lang.String interpret(java.lang.String text,
                                         java.util.Map substitutions)
Primitive variable substitution for strings. For any key x in the substitutions map the corresponding value will replace any subtrings of the form {x} int the text.

Parameters:
text - the string to be changed
substitutions - maps patterns to replacements.
Returns:
the changed string.

join

public static java.lang.String join(java.util.List list,
                                    java.lang.String separator)
Joins a List [a, b, c] into a string "a|b|c". Nulls are shown as empty strings ("a||c").

Parameters:
list - the list to join. Must not be null.
separator - will be inserted between the joined elements. If separator is null the string "null" will be inserted. To insert nothing provide the empty string.
Returns:
the joined string.
Throws:
java.lang.NullPointerException - if list is null.

join

public static java.lang.String join(java.lang.Object[] objects,
                                    java.lang.String delimiter)
See join(List, String).


loadFile

public static java.lang.String loadFile(java.lang.String filename)
                                 throws java.io.IOException
Load the contents of a file as a string.

Parameters:
filename - the name of the file to load.
Returns:
the contents of the file as a string.
Throws:
java.io.IOException - if the file indicated cannot be accessed.

saveFile

public static void saveFile(java.lang.String filename,
                            java.lang.String content)
                     throws java.io.IOException
Throws:
java.io.IOException

loadReader

public static java.lang.String loadReader(java.io.Reader reader)
                                   throws java.io.IOException
Loads the content of reader into a String.

Parameters:
reader - The reader to empty;
Returns:
the read string;
Throws:
java.io.IOException - if the file indicated cannot be accessed.

loadResource

public static java.lang.String loadResource(java.lang.String name,
                                            java.lang.Class clazz)
                                     throws java.lang.Exception
Load a resource (file) on the classpath as a string.

Parameters:
name - the fully qualified name of the file to load. E.g. /com/acme/stardust.xml, the name only if the file is resides in the same package as clazz.
clazz - the class in whose package the resource will be sought.
Returns:
the loaded resource as a string.
Throws:
java.lang.Exception - if the resource is not found.

loadResource

public static java.lang.String loadResource(java.lang.String name)
                                     throws java.lang.Exception
Load a resource (file) on the classpath as a string.

Parameters:
name - the fully qualified name of the file to load. E.g. /com/acme/stardust.xml (or the resource must reside in the same package as this class.
Returns:
the loaded resource as a string.
Throws:
java.lang.Exception - If the resource is not found or cannot be accessed.

loadStream

public static java.lang.String loadStream(java.io.InputStream in)
                                   throws java.io.IOException
Load all content from the given stream as string.

Parameters:
in - the stream to load from.
Returns:
the content of the stream.
Throws:
java.io.IOException - thrown if the Stream throws it.

repeat

public static java.lang.String repeat(java.lang.String s,
                                      int n)
Repeat the given string n times. precondition: n >= 0; Un-optimized.

Parameters:
s - the string to repeat
n - the number of times to repeat
Returns:
the repeated string.

replace

public static java.lang.String replace(java.lang.String text,
                                       java.lang.String pattern,
                                       java.lang.String replacement)
Substitutes all occurances of a pattern in a text replacementwith replacement.

Parameters:
text - the string to be changed
pattern - the substring of text to be replaced
replacement - the string to replace pattern in text.
Returns:
the affected string

replace

public static java.lang.String replace(java.lang.String text,
                                       java.lang.String pattern,
                                       java.lang.String replacement,
                                       int count)
Substitutes the first count occurances of a pattern in a text with replacement. Note that the empty string matches anything so the replacement will be repeated count times

Parameters:
text - the string to be changed
pattern - the substring of text to be replaced must be a nonempty string.
replacement - the string to replace pattern in text. Must not be null.
count - how many of the first instances of pattern to replace.
Returns:
the resulting string

split

public static java.util.List split(java.lang.String text,
                                   java.lang.String separator)
Splits a string "a|b|c" into list with elements ["a", "b", "c"] . Empty strings on either side of the separator are correctly identified and added to the list. An empty string without a separator returns an empty list as does null

Parameters:
text - the entities to put in the list separated by the separator.
separator - separates the entities to put in the list.
Returns:
A list. (never null).

substring

public static java.lang.String substring(java.lang.String text,
                                         int start,
                                         int stop)
Tolerant substring. Text can be null and stop can be beyound the end and start can be beyond stop. Start can be negative.

Parameters:
text - the string to take the substring from. Can be null .
start - the position of the first character of the substring in text. Can be less than stop, in which case it is set to the value of stop .
stop - the position after the last character of the substring in text. Can be beyond the length of text, in which case it is set to the length text.
Returns:
the substring if any, otherwise "".

substring

public static java.lang.String substring(java.lang.String text,
                                         int start)
Tolerant substring. Text can be null. Start can be negative.

Parameters:
text - the string to take the substring from. Can be null .
start - the position of the first character of the substring in text. Can be negative, in which case it is set to null. Can be beyond the end of the string in which case it is set to the length of the string.
Returns:
the part of text from start to the end of the string. Null if text is null.

toList

public static java.util.List toList(java.util.Enumeration e)
Enlists an enumeration.

Parameters:
e - the enumeration to enlist.
Returns:
the enlisted enumeration.

arrayToString

public static java.lang.String arrayToString(java.lang.String[] values,
                                             char separator)
Converts String array to a string with delimiter. Ignores elements that are null, "" or "-1".

Parameters:
values - to be concatenated.
separator - inserted between values in the returned string.
Returns:
the concatenated values if any or null otherwise.

removeNull

public static java.lang.String removeNull(java.lang.String value)
Returns the empty string if value is null or "null".

Parameters:
value - to be converted.
Returns:
the possibly converted string.

toNonNullString

public static java.lang.String toNonNullString(java.lang.Object object)
Converts null strings to "", all others objects to their Object.toString().

Parameters:
object - the object to stringify.
Returns:
Returns an empty string if the given string is null otherwise returns the givent string.

toStringArray

public static java.lang.String[] toStringArray(java.util.List list)
Converts a List of Objects into a array of strings.

Parameters:
list - the list to turn into an array. Null values become "null". Other values become their toString() results.
Returns:
the array of strings.

tokenize

public static java.lang.String[] tokenize(java.lang.String text,
                                          java.lang.String delimiters,
                                          boolean returndelimiters)
Splits the text on the delimiters and returns an array of the strings. If returndelimiters is true, then the delimiters are are also returned as tokens.

Note: Since it uses StringTokenizer, empty strings are simply skipped and do not appear in the list. Use split for more consistent behaviour.

Parameters:
text - the text to split
delimiters - on which to split
returndelimiters - true to get the delimiters also.
Returns:
an array of the split strings.

verify

public static void verify(boolean condition,
                          java.lang.String message)
                   throws java.lang.Exception
Throws an exception with the given message if the condition is true.

Parameters:
condition - the condition to test.
message - The message to give the exception.
Throws:
java.lang.Exception - if condition is true.

matches

public static boolean matches(java.lang.String text,
                              java.lang.String pattern)
Indicates wether the pattern matches the text. The only wildcard allowed is the asterisk.

Parameters:
text - the text to check for a match.
pattern - the pattern to match to text. An asterisk stands for zero or more characters.
Returns:
true if the pattern matches the text .

getStackTrace

public static java.lang.String getStackTrace(java.lang.Throwable t)
Returns the stack-trace of a Throwable as String.

Parameters:
t - the Throwable to trace:
Returns:
a string showing the stack when the exception occured.

front

public static java.lang.String front(java.lang.String text,
                                     java.lang.String sep)
Returns the part of text before sep. If sep is not part of text the whole text is returned.

Parameters:
text - The text to get the front off.
sep - separating the front from the rest.
Returns:
the part of text in front of sep.

lastFront

public static java.lang.String lastFront(java.lang.String text,
                                         java.lang.String sep)
Returns the part of text before the last sep. If sep is not part of text the whole text is returned.

Parameters:
text - The text to get the front off.
sep - separating the front from the rest.
Returns:
the part of text in front of sep.

rest

public static java.lang.String rest(java.lang.String text,
                                    java.lang.String sep)
Returns the rest of text after first sep. If sep is not part of test "" is returned.

Parameters:
text - The text to get the rest off.
sep - separating the rest from the front.
Returns:
the part of text after sep.

lastRest

public static java.lang.String lastRest(java.lang.String text,
                                        java.lang.String sep)
Returns the rest of text after last sep. If sep is not part of test "" is returned.

Parameters:
text - The text to get the rest off.
sep - separating the rest from the front.
Returns:
the part of text after sep.

makeList

public static java.util.List makeList()
A convenience method to make an empty list.


makeList

public static java.util.List makeList(java.lang.Object arg1)
A convenience method to make a one element list.


makeList

public static java.util.List makeList(java.lang.Object arg1,
                                      java.lang.Object arg2)
A convenience method to make a two element list.


makeList

public static java.util.List makeList(java.lang.Object arg1,
                                      java.lang.Object arg2,
                                      java.lang.Object arg3)
A convenience method to make a three element list.


makeList

public static java.util.List makeList(java.lang.Object arg1,
                                      java.lang.Object arg2,
                                      java.lang.Object arg3,
                                      java.lang.Object arg4)
A convenience method to make a four element list.


makeList

public static java.util.List makeList(java.lang.Object[] args)
A convenience method to enlist an array.


makeMap

public static java.util.Map makeMap(java.lang.Object key1,
                                    java.lang.Object value1)
A convenience method to make a one entry map.


makeMap

public static java.util.Map makeMap(java.lang.Object key1,
                                    java.lang.Object value1,
                                    java.lang.Object key2,
                                    java.lang.Object value2)
A convenience method to make a two entry map.


makeMap

public static java.util.Map makeMap(java.lang.Object key1,
                                    java.lang.Object value1,
                                    java.lang.Object key2,
                                    java.lang.Object value2,
                                    java.lang.Object key3,
                                    java.lang.Object value3)
A convenience method to make a three entry map.


makeMap

public static java.util.Map makeMap(java.lang.Object key1,
                                    java.lang.Object value1,
                                    java.lang.Object key2,
                                    java.lang.Object value2,
                                    java.lang.Object key3,
                                    java.lang.Object value3,
                                    java.lang.Object key4,
                                    java.lang.Object value4)
A convenience method to make a four entry map.


wrap

public static java.util.List wrap(java.lang.String prefix,
                                  java.util.List list,
                                  java.lang.String suffix)
Returns:
a new list with each element of the given list wrapped between the prefix and postfix strings.

Structured Text Toolkit