dsto.dfc.text
Class StringUtility

java.lang.Object
  extended by dsto.dfc.text.StringUtility

public final class StringUtility
extends Object

Utilities for manipulating strings.

Version:
$Revision$

Method Summary
static String capitaliseAllWords(String title)
          Capitalises all characters preceeded by a space, plus the first character of sentence.
static String capitaliseFirst(String str)
          Capitalise the first character of a string.
static String characters(char c, int count)
          Generate a string containing count number of character c.
static String escapeCharacters(String str, String chars)
          Escape a given set of special characters in a string using '\' as the escape character.
static String escapeCharacters(String str, String chars, char escapeChar)
          Escape a given set of special characters in a string.
static String expandSymbols(String str, Map symbols)
          Expand symbolic variables embedded within a string.
static String expandSymbols(String str, Map symbols, String symbolPrefixes, boolean useDelimeters)
          Expand symbolic variables embedded within a string.
static Object fromString(Class type, String string)
          Uses translatorFor(Class) to convert a string to a given value.
static boolean hasTranslatorFor(Class type)
          Shortcut to test whether a translator is registered for a given type.
static int indexOfChar(String str, String chars)
          Return the first character index in a string that is one of a specified set of characters.
static int indexOfChar(String str, String chars, int start)
          Return the first character index in a string that is one of a specified set of characters.
static boolean isHexString(String string)
          Test of string contains only hexadecimal characters (0-9,a-f).
static String join(List strings, String delimiter)
          Join an array of objects into single string.
static String join(Object[] objects, String delimiter)
          Join an array of objects into single string.
static String replace(String srcString, String replaceStr, String replaceWithStr)
          Replace all occurrences of one string with another.
static String[] split(String str)
           
static String[] split(String string, char[] delimiters)
          Splits a String up using the delimiter chars in delimiters into an array of Strings.
static String[] split(String string, char[] delimiters, boolean includeDelimiters)
          Splits a String up using the delimiter chars in delimiters into an array of Strings.
static String[] split(String str, String delimiter)
          Split a string into an array of substrings using a delimiter.
static String toString(Object value)
          Uses translatorFor(Class) to convert a value to a string.
static IStringTranslator translatorFor(Class type)
          Get a registered string translator for a given type.
static String truncate(String string, int maxLength)
          Truncate a string to a given max length.
static String wordifyCaps(String str)
          Turn a string like "HelloThereFrodo" into "Hello There Frodo".
static String wrappedHtml(String string, int width)
          Convert the given string into a multi-line word-wrapped string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

split

public static String[] split(String str)

split

public static String[] split(String str,
                             String delimiter)
Split a string into an array of substrings using a delimiter.

NB This is different to (JDK1.4+) String.split(String) for which the delimiter parameter is, in fact, a regular expression.

See Also:
join(Object[], String), String.split(java.lang.String)

split

public static String[] split(String string,
                             char[] delimiters)
Splits a String up using the delimiter chars in delimiters into an array of Strings. The delimiters are included in the Strings by default.

Parameters:
string - The String to split up.
delimiters - The delimiter chars to use.
Returns:
An array of Strings, which include the delimiter chars.
See Also:
split(String, char[], boolean)

split

public static String[] split(String string,
                             char[] delimiters,
                             boolean includeDelimiters)
Splits a String up using the delimiter chars in delimiters into an array of Strings. The delimiters are included in the Strings if includeDelimiters is true.

Parameters:
string - The String to split up.
delimiters - The delimiter chars to use.
includeDelimiters - If true, the delimiters are included in the resulting Strings.
Returns:
An array of Strings, which may or may not include the delimiter chars depending on if includeDelimiters is true or false.
See Also:
split(String, char[])

join

public static String join(Object[] objects,
                          String delimiter)
Join an array of objects into single string.

Parameters:
objects - The objects to join.
delimiter - The string to add between each pair of strings.
See Also:
split(java.lang.String)

join

public static String join(List strings,
                          String delimiter)
Join an array of objects into single string.

Parameters:
strings - The objects to join.
delimiter - The string to add between each pair of strings.
See Also:
split(java.lang.String)

capitaliseFirst

public static String capitaliseFirst(String str)
Capitalise the first character of a string.


replace

public static String replace(String srcString,
                             String replaceStr,
                             String replaceWithStr)
Replace all occurrences of one string with another.

Parameters:
srcString - The string to be transformed.
replaceStr - The string to replace.
replaceWithStr - The string to replace all occurrences of replaceStr.
Returns:
The transformed string.

expandSymbols

public static String expandSymbols(String str,
                                   Map symbols)
Expand symbolic variables embedded within a string. Each occurrence of a variable such as ${var} (alternatively can use

Parameters:
str - The string to expand.
symbols - The symbol definitions.
Returns:
The expanded string.

expandSymbols

public static String expandSymbols(String str,
                                   Map symbols,
                                   String symbolPrefixes,
                                   boolean useDelimeters)
Expand symbolic variables embedded within a string. Each occurrence of a variable such as ${var} in the string will be expanded to the result of symbols.get ("var") or "" if var is not defined in the property set.

Parameters:
str - The string to expand.
symbols - The symbol definitions.
symbolPrefixes - The set of character prefixes that variables may begin with (eg "$%").
useDelimeters - True if {}'s are allowed as symbol name delimeters, allowing multi-character names. When true, single character symbols may still be specified without {}'s.
Returns:
The expanded string.

indexOfChar

public static int indexOfChar(String str,
                              String chars)
Return the first character index in a string that is one of a specified set of characters.

Parameters:
str - The string to search.
chars - The set of characters to match.
Returns:
The first index of one of chars in str, or -1 if no match.
See Also:
indexOfChar(String, String, int)

indexOfChar

public static int indexOfChar(String str,
                              String chars,
                              int start)
Return the first character index in a string that is one of a specified set of characters.

Parameters:
str - The string to search.
chars - The set of characters to match.
start - The starting index to search from.
Returns:
The first index of one of chars in str, or -1 if no match.

wordifyCaps

public static String wordifyCaps(String str)
Turn a string like "HelloThereFrodo" into "Hello There Frodo".


isHexString

public static boolean isHexString(String string)
Test of string contains only hexadecimal characters (0-9,a-f).


escapeCharacters

public static String escapeCharacters(String str,
                                      String chars)
Escape a given set of special characters in a string using '\' as the escape character.

Parameters:
str - The string to escape.
chars - The set of special characters to escape (eg "'). The escape character itself is implicitly part of this set.
Returns:
The escaped string, or the original string if no special characters were present.
See Also:
escapeCharacters(String, String, char)

escapeCharacters

public static String escapeCharacters(String str,
                                      String chars,
                                      char escapeChar)
Escape a given set of special characters in a string.

Parameters:
str - The string to escape.
chars - The set of special characters to escape (eg "'). The escape character itself is implicitly part of this set.
escapeChar - The escape character (eg '\').
Returns:
The escaped string, or the original string if no special characters were present.

truncate

public static String truncate(String string,
                              int maxLength)
Truncate a string to a given max length.

Parameters:
string - The string to truncate. May be null.
maxLength - The max length of the returned string. If longer, the string is truncated with "...". The minimum string length will never be less than 3.

capitaliseAllWords

public static String capitaliseAllWords(String title)
Capitalises all characters preceeded by a space, plus the first character of sentence.

Parameters:
title - The sentence, each word of which to capitalise.
Returns:
A copy of sentence, but with each word capitalised.

characters

public static String characters(char c,
                                int count)
Generate a string containing count number of character c.


fromString

public static Object fromString(Class type,
                                String string)
                         throws InvalidFormatException
Uses translatorFor(Class) to convert a string to a given value.

Parameters:
type - The target type to convert to.
string - The string to convert.
Returns:
an instance of type created by parsing the string.
Throws:
InvalidFormatException - if an error occurs parsing.

toString

public static String toString(Object value)
                       throws IllegalArgumentException
Uses translatorFor(Class) to convert a value to a string. This may be different from simply calling toString() depending on which translator is registered for the value class.

Parameters:
value - The value to covert.
Returns:
The string value.
Throws:
IllegalArgumentException - if value has no associated translator.

hasTranslatorFor

public static boolean hasTranslatorFor(Class type)
Shortcut to test whether a translator is registered for a given type.

See Also:
translatorFor(Class)

translatorFor

public static IStringTranslator translatorFor(Class type)
                                       throws IllegalArgumentException
Get a registered string translator for a given type.

Parameters:
type - The type to look for.
Returns:
The translator for type.
Throws:
IllegalArgumentException - if there is no translator for type.
See Also:
hasTranslatorFor(Class)

wrappedHtml

public static String wrappedHtml(String string,
                                 int width)
Convert the given string into a multi-line word-wrapped string. The function does not cut the words so the tooltip created will generally be slightly wider than the given width.

Parameters:
string - The string to word wrap
width - The interval used to wrap words
Returns:
A string containing a word wrapped version of the given string.


Copyright © 2008 Commonwealth of Australia