org.tentackle.util
Class StringHelper

java.lang.Object
  extended by org.tentackle.util.StringHelper

public class StringHelper
extends Object

Some handy methods for strings.

Author:
harald

Field Summary
static char creditLetter
          short letter for "credit" (haben)
static DateFormat dateFormat
          localized DateFormat for a date
static String datePattern
          localized format string for a date
static char debitLetter
          short letter for "debit" (soll)
static String emptyString
          the empty string
static String floatDoublePattern
          default float/double format string
static String integerPattern
          default integer format string
static String lineSeparatorString
          the newline string
static char[] MD5SALT
          Salt for md5sum(char[]).
The default is null, i.e.
static String moneyPattern
          default money format string
static DateFormat shortDateFormat
          localized DateFormat for a short date
static String shortDatePattern
          localized format string for a short date
static DateFormat shortTimeFormat
          localized DateFormat for a short time
static String shortTimePattern
          localized format string for a short time
static DateFormat shortTimestampFormat
          localized DateFormat for a short timestamp
static String shortTimestampPattern
          localized format string for a short timestamp
static StringConverter stringNormalizer
          The default normalizer used within the application.
static DateFormat timeFormat
          localized DateFormat for a time
static String timePattern
          localized format string for a time
static DateFormat timestampFormat
          localized DateFormat for a timestamp
static String timestampPattern
          localized format string for a timestamp
 
Constructor Summary
StringHelper()
           
 
Method Summary
static String debitCreditToString(BMoney money, boolean debit)
          Translates a money value to a string with a suffix indicating whether debit or credit.
static String firstLine(String str)
          Gets the first line from a multi-line string.
static String getClassBaseName(Class<?> clazz)
          Gets the basename of a class.
static String getClassBaseName(String str)
          Gets the basename of a classname(-like) string.
static boolean isAllDigits(String str)
          checks if a string contains only digits, i.e.
static boolean isAllDigits(String str, boolean whitespaceAllowed)
          Checks if a string contains only digits or whitespaces, i.e.
static boolean isAllWhitespace(String str)
          Checks if string contains only whitespaces.
static void loadFormats()
          Loads the localized formats.
static String md5sum(char[] input)
          Builds an MD5-sum from an array of chars with a default salt given by MD5SALT.
static String md5sum(char[] salt, char[] input)
          Builds an MD5-sum from an array of chars as used in password-fields.
Note that this method converts the characters to bytes via toBytes(char[]) before applying the md5 hash.
static String normalize(String str)
          Normalizes a string (phonetically) for use as AppDbObject.normText using stringNormalizer.
static String normalize(String str, StringConverter normalizer)
          Normalizes a string (phonetically) for use as AppDbObject.normText.
static String objectArrayToString(Object[] objArray, String separator)
          Creates a string from an object array.
static byte[] toBytes(char c)
          Converts a single (unicode) char to a byte-array.
static byte[] toBytes(char[] chars)
          Converts a char-array to a byte-array.
static char[] toCharArray(String str)
          Transforms a string to a char-array.
static String toDoubleQuotes(String str)
          Takes a string, surrounds it with double-quotes and escapes all double-quotes already in the string according to Unix rules.
static String toDTA(String str)
          Filters illegal chars for DTA-Files (German "DATENAUSTAUSCH" banking format).
static String toFixedLength(String str, int length)
          Takes a string and returns one with a given length, cutting or filling up with spaces, whatever appropriate.
static String toFixedLength(String str, int length, char filler)
          Takes a string and returns one with a given length, cutting or filling up with fillchars, whatever appropriate.
static String toFixedLengthLeftFill(String str, int length)
          Takes a string and returns one with a given length, cutting or filling up with spaces from the left, whatever appropriate.
static String toFixedLengthLeftFill(String str, int length, char filler)
          Takes a string and returns one with a given length, cutting or filling up with fillchars from the left, whatever appropriate.
static String toHexString(byte[] binaryData)
          Creates a human-readable hex-String out of a byte-array (e.g.
static String toHTML(String text)
          Converts a multiline string to an HTML-string that can be displayed in a label.
static String toString(String str)
          Maps null to the empty string.
static String toVarName(String str)
          Filters illegal chars for Java variable names.
static String toWindowsDoubleQuotes(String str)
          Takes a string, surrounds it with double-quotes and escapes all double-quotes already in the string according to Windows-rules.
static String trim(String s)
          Trims a string.
static String trim(String s, int max)
          Trims a string.
static String unDiacrit(String str)
          Converts special unicode characters (so-called diacrits) to standard ascii.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

integerPattern

public static final String integerPattern
default integer format string

See Also:
Constant Field Values

floatDoublePattern

public static final String floatDoublePattern
default float/double format string

See Also:
Constant Field Values

moneyPattern

public static final String moneyPattern
default money format string

See Also:
Constant Field Values

timestampPattern

public static String timestampPattern
localized format string for a timestamp


datePattern

public static String datePattern
localized format string for a date


timePattern

public static String timePattern
localized format string for a time


shortTimestampPattern

public static String shortTimestampPattern
localized format string for a short timestamp


shortDatePattern

public static String shortDatePattern
localized format string for a short date


shortTimePattern

public static String shortTimePattern
localized format string for a short time


timestampFormat

public static DateFormat timestampFormat
localized DateFormat for a timestamp


dateFormat

public static DateFormat dateFormat
localized DateFormat for a date


timeFormat

public static DateFormat timeFormat
localized DateFormat for a time


shortTimestampFormat

public static DateFormat shortTimestampFormat
localized DateFormat for a short timestamp


shortDateFormat

public static DateFormat shortDateFormat
localized DateFormat for a short date


shortTimeFormat

public static DateFormat shortTimeFormat
localized DateFormat for a short time


debitLetter

public static char debitLetter
short letter for "debit" (soll)


creditLetter

public static char creditLetter
short letter for "credit" (haben)


emptyString

public static final String emptyString
the empty string

See Also:
Constant Field Values

lineSeparatorString

public static final String lineSeparatorString
the newline string

See Also:
Constant Field Values

stringNormalizer

public static StringConverter stringNormalizer
The default normalizer used within the application. Defaults to DefaultStringNormalizer.


MD5SALT

public static char[] MD5SALT
Salt for md5sum(char[]).
The default is null, i.e. no salt. Applications should set an individual salt to enhance security.

Constructor Detail

StringHelper

public StringHelper()
Method Detail

loadFormats

public static void loadFormats()
Loads the localized formats. Invoked whenever the Locale changes.


unDiacrit

public static String unDiacrit(String str)
Converts special unicode characters (so-called diacrits) to standard ascii. Supports also special german and northern european "umlauts".

Parameters:
str - the string to be converted
Returns:
the converted string

normalize

public static String normalize(String str,
                               StringConverter normalizer)
Normalizes a string (phonetically) for use as AppDbObject.normText.

Parameters:
str - the string to be normalized
normalizer - the normalizer to use, null = global default
Returns:
the normalized string

normalize

public static String normalize(String str)
Normalizes a string (phonetically) for use as AppDbObject.normText using stringNormalizer.

Parameters:
str - the string to be normalized
Returns:
the normalized string

toDoubleQuotes

public static String toDoubleQuotes(String str)
Takes a string, surrounds it with double-quotes and escapes all double-quotes already in the string according to Unix rules. Example:
 Length 5" --> "Length 5\""
 

Parameters:
str - the string
Returns:
the string in double quotes

toWindowsDoubleQuotes

public static String toWindowsDoubleQuotes(String str)
Takes a string, surrounds it with double-quotes and escapes all double-quotes already in the string according to Windows-rules. Example:
 Length 5" --> "Length 5""
 

Parameters:
str - the string
Returns:
the string in double quotes

toFixedLength

public static String toFixedLength(String str,
                                   int length,
                                   char filler)
Takes a string and returns one with a given length, cutting or filling up with fillchars, whatever appropriate.

Parameters:
str - the string
length - the length of the returned string
filler - the character to fill up if str is too short
Returns:
the string with the desired length

toFixedLength

public static String toFixedLength(String str,
                                   int length)
Takes a string and returns one with a given length, cutting or filling up with spaces, whatever appropriate.

Parameters:
str - the string
length - the length of the returned string
Returns:
the string with the desired length

toFixedLengthLeftFill

public static String toFixedLengthLeftFill(String str,
                                           int length,
                                           char filler)
Takes a string and returns one with a given length, cutting or filling up with fillchars from the left, whatever appropriate.

Parameters:
str - the string
length - the length of the returned string
filler - the character to fill up if str is too short
Returns:
the string with the desired length

toFixedLengthLeftFill

public static String toFixedLengthLeftFill(String str,
                                           int length)
Takes a string and returns one with a given length, cutting or filling up with spaces from the left, whatever appropriate.

Parameters:
str - the string
length - the length of the returned string
Returns:
the string with the desired length

toDTA

public static String toDTA(String str)
Filters illegal chars for DTA-Files (German "DATENAUSTAUSCH" banking format).

Parameters:
str - the string
Returns:
the DTA conform string

toVarName

public static String toVarName(String str)
Filters illegal chars for Java variable names.

Parameters:
str - the string
Returns:
the java conform string

isAllDigits

public static boolean isAllDigits(String str,
                                  boolean whitespaceAllowed)
Checks if a string contains only digits or whitespaces, i.e. no illegal char in a number string.

Parameters:
str - the string to check
whitespaceAllowed - true if whitespaces are allowed
Returns:
true if no illegal char detected, false otherwise

isAllDigits

public static boolean isAllDigits(String str)
checks if a string contains only digits, i.e. no non-number char in string, even no whitespace.

Parameters:
str - the string to check
Returns:
true if no illegal char detected, false otherwise

debitCreditToString

public static String debitCreditToString(BMoney money,
                                         boolean debit)
Translates a money value to a string with a suffix indicating whether debit or credit. Commonly used in financial accounting applications.

Parameters:
money - the amount of money
debit - true if the amount is debit
Returns:
the formatted value

trim

public static String trim(String s,
                          int max)
Trims a string. The method is nullpointer safe.

Parameters:
s - the string, may be null
max - the maximum number of characters, 0 = minimum length
Returns:
the trimmed string, null if s == null

trim

public static String trim(String s)
Trims a string. The method is nullpointer safe.

Parameters:
s - the string, may be null
Returns:
the trimmed string, null if s == null
See Also:
trim(java.lang.String, int)

isAllWhitespace

public static boolean isAllWhitespace(String str)
Checks if string contains only whitespaces.

Parameters:
str - the string to check
Returns:
true if null, empty or all whitespace, false if at least one non-whitespace-character found

firstLine

public static String firstLine(String str)
Gets the first line from a multi-line string. Nice in tables.

Parameters:
str - the multiline string
Returns:
the first line, null if str == null

toHexString

public static String toHexString(byte[] binaryData)
Creates a human-readable hex-String out of a byte-array (e.g. from MessageDigest MD5sum).

Parameters:
binaryData - the data, may be null
Returns:
the formatted hex string , null if data was null

toBytes

public static byte[] toBytes(char c)
Converts a single (unicode) char to a byte-array.

Parameters:
c - the character
Returns:
the byte[2] array

toBytes

public static byte[] toBytes(char[] chars)
Converts a char-array to a byte-array.

Parameters:
chars - the character array
Returns:
the byte array

md5sum

public static String md5sum(char[] salt,
                            char[] input)
Builds an MD5-sum from an array of chars as used in password-fields.
Note that this method converts the characters to bytes via toBytes(char[]) before applying the md5 hash. By this we achieve an enhanced security against md5 crackers, which assume ordinary strings. In order to enhance security even more, however, you should add some application-specific salt.

Parameters:
salt - the "salt", null if plain MD5.
input - is the input array of chars
Returns:
the md5sum as a string, null if input == null or conversion failed (error is logged)
See Also:
md5sum(char[]), MD5SALT

md5sum

public static String md5sum(char[] input)
Builds an MD5-sum from an array of chars with a default salt given by MD5SALT.

Parameters:
input - is the input array of chars
Returns:
the md5sum as a string, null if input == null or conversion failed (error is logged)

toString

public static String toString(String str)
Maps null to the empty string. Simple but essential ;)

Parameters:
str - the string to test against null
Returns:
str or the emptystring if str is null

getClassBaseName

public static String getClassBaseName(String str)
Gets the basename of a classname(-like) string. The basename is the last name of a pathname with dots.

Parameters:
str - the classname
Returns:
the basename

getClassBaseName

public static String getClassBaseName(Class<?> clazz)
Gets the basename of a class. The basename is the class name without the package.

Parameters:
clazz - the class
Returns:
the basename
See Also:
getClassBaseName(java.lang.String)

objectArrayToString

public static String objectArrayToString(Object[] objArray,
                                         String separator)
Creates a string from an object array.

Parameters:
objArray - the array of objects
separator - the string between two objects
Returns:
the string

toCharArray

public static char[] toCharArray(String str)
Transforms a string to a char-array. nullp-safe.

Parameters:
str - the string
Returns:
the character array

toHTML

public static String toHTML(String text)
Converts a multiline string to an HTML-string that can be displayed in a label. Useful to print multiline labels.

Parameters:
text - the input string
Returns:
the HTML string


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de