org.openbp.common.string
Class StringUtil

java.lang.Object
  extended by org.openbp.common.string.StringUtil

public final class StringUtil
extends java.lang.Object

This class contains various static string utilitiy methods. In addition, it contains some other methods of very generic type.

Author:
Heiko Erhardt

Field Summary
static java.lang.String FOLDER_SEP
          Folder separator for usage withing resource and file system paths
static char FOLDER_SEP_CHAR
          Folder separator for usage withing resource and file system paths
 
Method Summary
static java.lang.String absolutePathName(java.lang.String s)
          Converts a path name to its absolute form.
static void append(java.lang.StringBuffer sb, java.lang.String s1, java.lang.String s2)
          Appends 2 strings to a string buffer.
static void append(java.lang.StringBuffer sb, java.lang.String s1, java.lang.String s2, java.lang.String s3)
          Appends 3 strings to a string buffer.
static void append(java.lang.StringBuffer sb, java.lang.String s1, java.lang.String s2, java.lang.String s3, java.lang.String s4)
          Appends 4 strings to a string buffer.
static void append(java.lang.StringBuffer sb, java.lang.String s1, java.lang.String s2, java.lang.String s3, java.lang.String s4, java.lang.String s5)
          Appends 5 strings to a string buffer.
static void appendLine(java.lang.StringBuffer sb, java.lang.String s1)
          Append a string and a new line character to a string buffer.
static void appendLine(java.lang.StringBuffer sb, java.lang.String s1, java.lang.String s2)
          Append 2 strings and a new line character to a string buffer.
static void appendLine(java.lang.StringBuffer sb, java.lang.String s1, java.lang.String s2, java.lang.String s3)
          Append 3 strings and a new line character to a string buffer.
static void appendLine(java.lang.StringBuffer sb, java.lang.String s1, java.lang.String s2, java.lang.String s3, java.lang.String s4)
          Append 4 strings and a new line character to a string buffer.
static void appendLine(java.lang.StringBuffer sb, java.lang.String s1, java.lang.String s2, java.lang.String s3, java.lang.String s4, java.lang.String s5)
          Append 5 strings and a new line character to a string buffer.
static java.lang.String buildPath(java.lang.String p1, java.lang.String p2)
          Builds a result path from two path components.
static java.lang.String capitalize(java.lang.String name)
          Uppercases the first character of a string.
static boolean contains(java.lang.String s, java.lang.String[] list)
          Checks if the given string list contains the given string.
static java.lang.String decapitalize(java.lang.String name)
          Lowercases the first character of a string.
static java.lang.String detokenize(java.lang.String[] strings, char separatorChar)
          This method returns a string constructed from each element in the passed string array separated by the passed character.
static int indexOfIgnoreCase(java.lang.String s, int startIndex, java.lang.String pattern)
          indexOf method that is not case-sensitive.
static java.lang.String int2String(int i)
          Converts an integer to its string representation.
static java.lang.String normalizeDir(java.lang.String path)
          Normalizes a directory specification.
static java.lang.String normalizePathName(java.lang.String path)
          Normalizes a resource or file name by removing all "/./" or "/../" references.
static char safeCharAt(java.lang.String s, int index)
           
static boolean string2boolean(java.lang.String value)
          Converts a string to its boolean representation.
static java.lang.String substitute(java.lang.String s, java.util.Map map)
          Substitutes alls occurences of several patterns in a string.
static java.lang.String substitute(java.lang.String s, java.lang.String pattern, java.lang.String replacement)
          Substitutes alls occurences of a pattern in a string.
static java.lang.String substituteIgnoreCase(java.lang.String s, java.util.Map map)
          Substitutes alls occurences of several patterns in a string.
static java.lang.String substituteIgnoreCase(java.lang.String s, java.lang.String pattern, java.lang.String replacement)
          Substitutes alls occurences of a pattern in a string, ignores character casing.
static java.lang.String[] tokenize(java.lang.String string, java.lang.String separator)
          This method tokenizer the passed string using the given separator and returns the resulting fragment as a String array.
static java.lang.String[] tokenize(java.lang.String string, java.lang.String separator, boolean trim)
          This method tokenizes the passed string using the given separator and returns the resulting fragment as a String array.
static java.lang.String trim(java.lang.String origin, char[] trimCharacters)
          Trims a string from characters.
static java.lang.String trimNull(java.lang.String s)
          Trims a string, returning null if the trimmed length of the string is 0.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FOLDER_SEP

public static final java.lang.String FOLDER_SEP
Folder separator for usage withing resource and file system paths

See Also:
Constant Field Values

FOLDER_SEP_CHAR

public static final char FOLDER_SEP_CHAR
Folder separator for usage withing resource and file system paths

See Also:
Constant Field Values
Method Detail

tokenize

public static java.lang.String[] tokenize(java.lang.String string,
                                          java.lang.String separator)
This method tokenizer the passed string using the given separator and returns the resulting fragment as a String array. Calls tokenize(String, String)

Parameters:
string - The string to be tokenized
separator - The separator
Returns:
The fragments of the string

tokenize

public static java.lang.String[] tokenize(java.lang.String string,
                                          java.lang.String separator,
                                          boolean trim)
This method tokenizes the passed string using the given separator and returns the resulting fragment as a String array.

Parameters:
string - The string to be tokenized
separator - The separator
trim - true The tokens have been trimmed before the were added
false The raw tokens will be used
Returns:
The fragments of the string

detokenize

public static java.lang.String detokenize(java.lang.String[] strings,
                                          char separatorChar)
This method returns a string constructed from each element in the passed string array separated by the passed character.

Parameters:
strings - The strings to be concatenated
separatorChar - The character to be used for seperation
Returns:
The resulting string

indexOfIgnoreCase

public static int indexOfIgnoreCase(java.lang.String s,
                                    int startIndex,
                                    java.lang.String pattern)
indexOf method that is not case-sensitive.

Parameters:
s - Sting to operate on or null
startIndex - Index to start the search at
pattern - Pattern to search for
Returns:
Position of pattern or -1 if not found

substituteIgnoreCase

public static java.lang.String substituteIgnoreCase(java.lang.String s,
                                                    java.lang.String pattern,
                                                    java.lang.String replacement)
Substitutes alls occurences of a pattern in a string, ignores character casing.

Parameters:
s - String to work on or null
pattern - Pattern to search for
replacement - String to replace for the pattern
Returns:
The result string or s itself if the pattern was not found

substituteIgnoreCase

public static java.lang.String substituteIgnoreCase(java.lang.String s,
                                                    java.util.Map map)
Substitutes alls occurences of several patterns in a string. Ignores character casing when comparing the string to the patterns.

Parameters:
s - String to work on
map - Map containing the patterns as keys and the replacements as values
Returns:
The result string or s itself if the string does not contain any of the patterns

substitute

public static java.lang.String substitute(java.lang.String s,
                                          java.lang.String pattern,
                                          java.lang.String replacement)
Substitutes alls occurences of a pattern in a string.

Parameters:
s - String to work on or null
pattern - Pattern to search for
replacement - String to replace for the pattern or null for nothing
Returns:
The result string or s itself if the pattern was not found

substitute

public static java.lang.String substitute(java.lang.String s,
                                          java.util.Map map)
Substitutes alls occurences of several patterns in a string. The pattern comparisons case-dependent.

Parameters:
s - String to work on
map - Map containing the patterns as keys and the replacements as values
Returns:
The result string or s itself if the string does not contain any of the patterns

capitalize

public static java.lang.String capitalize(java.lang.String name)
Uppercases the first character of a string.

Parameters:
name - String to capitalize
Returns:
The capitalized string

decapitalize

public static java.lang.String decapitalize(java.lang.String name)
Lowercases the first character of a string.

Parameters:
name - String to decapitalize
Returns:
The decapitalized string

absolutePathName

public static java.lang.String absolutePathName(java.lang.String s)
Converts a path name to its absolute form. Also converts Windows path separator ('\') to '/'.

Parameters:
s - Path to normalize or null
Returns:
The normalized path or null

normalizePathName

public static java.lang.String normalizePathName(java.lang.String path)
Normalizes a resource or file name by removing all "/./" or "/../" references. Also converts Windows path separator ('\') to '/'.

Parameters:
path - Path to normalize or null
Returns:
The normalized path or null

normalizeDir

public static java.lang.String normalizeDir(java.lang.String path)
Normalizes a directory specification. Executes normalizePathName(java.lang.String) and removes leading or trailing folder delimiters. Also converts Windows path separator ('\') to '/'.

Parameters:
path - Path to normalize or null
Returns:
The normalized path or null

buildPath

public static java.lang.String buildPath(java.lang.String p1,
                                         java.lang.String p2)
Builds a result path from two path components. Leading or trailing path separtors will be considered

Parameters:
p1 - First path component or null
p2 - Second path component or null
Returns:
The resulting path or null

append

public static void append(java.lang.StringBuffer sb,
                          java.lang.String s1,
                          java.lang.String s2)
Appends 2 strings to a string buffer.


append

public static void append(java.lang.StringBuffer sb,
                          java.lang.String s1,
                          java.lang.String s2,
                          java.lang.String s3)
Appends 3 strings to a string buffer.


append

public static void append(java.lang.StringBuffer sb,
                          java.lang.String s1,
                          java.lang.String s2,
                          java.lang.String s3,
                          java.lang.String s4)
Appends 4 strings to a string buffer.


append

public static void append(java.lang.StringBuffer sb,
                          java.lang.String s1,
                          java.lang.String s2,
                          java.lang.String s3,
                          java.lang.String s4,
                          java.lang.String s5)
Appends 5 strings to a string buffer.


appendLine

public static void appendLine(java.lang.StringBuffer sb,
                              java.lang.String s1)
Append a string and a new line character to a string buffer.


appendLine

public static void appendLine(java.lang.StringBuffer sb,
                              java.lang.String s1,
                              java.lang.String s2)
Append 2 strings and a new line character to a string buffer.


appendLine

public static void appendLine(java.lang.StringBuffer sb,
                              java.lang.String s1,
                              java.lang.String s2,
                              java.lang.String s3)
Append 3 strings and a new line character to a string buffer.


appendLine

public static void appendLine(java.lang.StringBuffer sb,
                              java.lang.String s1,
                              java.lang.String s2,
                              java.lang.String s3,
                              java.lang.String s4)
Append 4 strings and a new line character to a string buffer.


appendLine

public static void appendLine(java.lang.StringBuffer sb,
                              java.lang.String s1,
                              java.lang.String s2,
                              java.lang.String s3,
                              java.lang.String s4,
                              java.lang.String s5)
Append 5 strings and a new line character to a string buffer.


safeCharAt

public static char safeCharAt(java.lang.String s,
                              int index)

trimNull

public static java.lang.String trimNull(java.lang.String s)
Trims a string, returning null if the trimmed length of the string is 0.

Parameters:
s - String to trim or null
Returns:
The trimmed string or null

int2String

public static java.lang.String int2String(int i)
Converts an integer to its string representation.


string2boolean

public static boolean string2boolean(java.lang.String value)
Converts a string to its boolean representation.

Parameters:
value - String value
Returns:
true The value string is 'true'
false The value string is 'false'.
Throws:
java.lang.IllegalArgumentException - The value string doesn't represent a boolean value

trim

public static java.lang.String trim(java.lang.String origin,
                                    char[] trimCharacters)
Trims a string from characters.

Parameters:
origin - String to trim
trimCharacters - Array of characters
Returns:
The trimmed string

contains

public static boolean contains(java.lang.String s,
                               java.lang.String[] list)
Checks if the given string list contains the given string.

Parameters:
s - String to search for or null
list - String list or null
Returns:
true If the string is not null and the string list contains the string false Otherwise


Copyright © 2011. All Rights Reserved.