org.molwind.util
Class Stringx

java.lang.Object
  extended by org.molwind.util.Stringx

public final class Stringx
extends java.lang.Object

The Stringx class provides a collection of static methods for extended string processing.

Version:
1.0
Author:
Oliver Karch

Method Summary
static java.lang.String after(java.lang.String source, java.lang.String delimiter)
          Tailors a source string to the string beginning after a given delimiter string.
static java.lang.String afterLast(java.lang.String source, java.lang.String delimiter)
          Determines the string's portion after the last occurrence of the delimiter.
static java.lang.String before(java.lang.String source, java.lang.String delimiter)
          Cuts a substring out of a source string ending at a given delimiter string.
static java.lang.String concat(java.lang.String source)
          Concatenates a paragraph of text into a single line by removing line ends and unnecessary blanks (i.e.
static java.lang.String currentDateString(java.lang.String format)
          Formats the current date according to the given format string.
static java.lang.String deleteAll(java.lang.String source, java.lang.String delete)
          Deletes all occurrences of a substring from a source string.
static java.lang.String deleteBetweenAll(java.lang.String source, java.lang.String leftMark, java.lang.String rightMark)
          Deletes all occurrences of a substring between a left delimiter and a right delimiter string.
static java.lang.String escapeChar(java.lang.String string)
          Replaces in the given string all meta character sequences such as \n, \t etc.
static java.lang.String escapeXmlAttributeValue(java.lang.String value)
          Escapes all xml meta-characters in the given string by the corresponding entity char sequences.
static java.lang.String extract(java.lang.String source, java.lang.String leftMark, java.lang.String rightMark)
          Extracts a substring from a source string between a left and a right delimiter string.
static java.lang.String[] extractAll(java.lang.String source, java.lang.String leftMark, java.lang.String rightMark)
          Extracts a substring from a source string between a left and a right delimiter string.
static java.lang.String fill(char character, int length)
          Constructs a string with the specified length that only consists of the given character.
static int fractionDigits(java.lang.String num)
          Counts the digits of the fraction of a number's string representation of a source string.
static java.lang.String getClassName(java.lang.Class clazz)
          Extracts the base class name (i.e.
static java.lang.String getDateString(java.lang.String format, java.util.Date date)
          Formats a given date according to the format string.
static java.lang.String getDefault(java.lang.String string, java.lang.String def)
          If the given string is null the default value provided is returned.
static boolean isHexDigit(char character)
          Checks whether a given char can be part of a hex string.
static boolean isIdentifier(java.lang.String token)
          Checks whether a given string follows the rules for identifiers or not.
static java.lang.String localDate()
          Formats the current date according to local formating conventions.
static java.lang.String localDate(long date)
          Formats the current date according to local formating conventions.
static java.lang.String localMediumDate(long date)
          Formats the current date according to local formating conventions to display medium format length.
static java.lang.String localTime()
          Formats the current time according to local formating conventions.
static java.lang.String localTime(long date)
          Formats the current time according to local formating conventions.
static long parseDate(java.lang.String dateString)
          Parses a date string according to local formating conventions.
static long parseDate(java.lang.String dateString, java.lang.String pattern)
          Parses a date string conforming to a given date format pattern.
static long parseTime(java.lang.String timeString)
          Parses a time string according to local formating conventions.
static java.lang.String strapp(java.lang.String source, java.lang.String append)
          Append a string to the source string if it has not been appended already.
static java.lang.String strbreak(java.lang.String source, java.lang.String insert, int length)
          Breaks up a string into smaller pieces of specified length appended by the given string.
static java.lang.String strcut(java.lang.String source, java.lang.String delimiter)
          Discards all characters of a source string after a delimiter string.
static java.lang.String strnewline(java.lang.String source, int length)
          Breaks up a string into smaller pieces of specified length appended by a newline.
static java.lang.String[] strtok(java.lang.String source)
          Determines all tokens of a source string that are delimited by whitespaces.
static java.lang.String[] strtok(java.lang.String source, java.lang.String delimiter)
          Determines all tokens of a source string that are delimited by characters of a delimiter string.
static java.lang.String[] strtok(java.lang.String source, java.lang.String delimiter, boolean trim)
          Determines all tokens of a source string that are delimited by characters of a delimiter string, optionally trims the tokens.
static java.lang.String strtrunc(java.lang.String source, int length)
          Truncates a string if it exceeds a certain length.
static java.lang.String strtrunc(java.lang.String source, int length, java.lang.String cont)
          Truncates a string if it exceeds a certain length and appends a given continuation string instead.
static java.lang.String substitute(java.lang.String source, java.lang.String substitute, java.lang.String replacement)
          Replaces one occurrence of a string to be substituted in a source string by a replacing string.
static java.lang.String substituteAll(java.lang.String source, java.lang.String substitute, java.lang.String replacement)
          Replaces all occurrences of a string to be substituted in a source string by a replacing string.
static java.lang.String subtoken(java.lang.String source, int index, java.lang.String delimiter)
          Extracts a substring from a source string beginning at a specified position and ending with a specific delimiter.
static boolean toBoolean(java.lang.String string, boolean def)
          Converts a String to a boolean value.
static java.awt.Color toColor(java.lang.String colorName)
          Converts a color name or description like #FFCC00 into a color object.
static long toCRC(java.lang.String string)
          Creates a crc checksum from a String.
static double toDouble(java.lang.String string, double def)
          Converts a String to a double value.
static float toFloat(java.lang.String string, float def)
          Converts a String to a float value.
static int toInt(java.lang.String string, int def)
          Converts a String to an int value.
static long toLong(java.lang.String string, long def)
          Converts a String to a long value.
static java.util.Properties toProperties(java.lang.String itemString, java.lang.String delimiter)
          Creates a Properties object from a key-value pair string which looks something like item1 = value1; item2 = value2; ...
static java.lang.String toStringList(java.util.Collection items, java.lang.String delimiter)
          Creates a string consisting of the array's items separated by the given delimiter.
static java.lang.String toStringList(java.lang.Object[] items, java.lang.String delimiter)
          Creates a string consisting of the array's items separated by the given delimiter.
static java.lang.String trimZero(java.lang.String source)
          Removes all leading non-digits and zeros from a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

currentDateString

public static java.lang.String currentDateString(java.lang.String format)
Formats the current date according to the given format string. For example a valid format string could be dd.MM.YYYY.

See SimpleDateFormat for formatting options.

Parameters:
format - the format string
Returns:
the formated date string

getDateString

public static java.lang.String getDateString(java.lang.String format,
                                             java.util.Date date)
Formats a given date according to the format string. For example a valid format string could be dd.MM.YYYY.

See SimpleDateFormat for formatting options.

Parameters:
format - the format string
date - the date
Returns:
the formated date string

localDate

public static java.lang.String localDate()
Formats the current date according to local formating conventions.

Returns:
the formated date string

localTime

public static java.lang.String localTime()
Formats the current time according to local formating conventions.

Returns:
the formated time string

localDate

public static java.lang.String localDate(long date)
Formats the current date according to local formating conventions.

Parameters:
date - milliseconds since era
Returns:
the formated date string

localMediumDate

public static java.lang.String localMediumDate(long date)
Formats the current date according to local formating conventions to display medium format length.

Parameters:
date - milliseconds since era
Returns:
the formated date string

parseDate

public static long parseDate(java.lang.String dateString)
Parses a date string according to local formating conventions.

Parameters:
dateString - a date string
Returns:
a long representing the date or -1 if an error occurred

parseDate

public static long parseDate(java.lang.String dateString,
                             java.lang.String pattern)
Parses a date string conforming to a given date format pattern.

Parameters:
dateString - a date string
pattern - a date pattern
Returns:
a long representing the date or -1 if an error occurred

localTime

public static java.lang.String localTime(long date)
Formats the current time according to local formating conventions.

Parameters:
date - milliseconds since era
Returns:
the formated date string

parseTime

public static long parseTime(java.lang.String timeString)
Parses a time string according to local formating conventions.

Parameters:
timeString - a time string
Returns:
a long representing the time or -1 if an error occurred

before

public static java.lang.String before(java.lang.String source,
                                      java.lang.String delimiter)
Cuts a substring out of a source string ending at a given delimiter string.

Parameters:
source - the source string
delimiter - a delimiter string
Returns:
the subtoken

after

public static java.lang.String after(java.lang.String source,
                                     java.lang.String delimiter)
Tailors a source string to the string beginning after a given delimiter string.

Parameters:
source - the source string
delimiter - a delimiter string
Returns:
the subtoken

afterLast

public static java.lang.String afterLast(java.lang.String source,
                                         java.lang.String delimiter)
Determines the string's portion after the last occurrence of the delimiter.

Parameters:
source - the source string
delimiter - a delimiter string
Returns:
the subtoken

strcut

public static java.lang.String strcut(java.lang.String source,
                                      java.lang.String delimiter)
Discards all characters of a source string after a delimiter string.

Parameters:
source - the source string
delimiter - a delimiter string
Returns:
the shortened string

strtok

public static java.lang.String[] strtok(java.lang.String source,
                                        java.lang.String delimiter)
Determines all tokens of a source string that are delimited by characters of a delimiter string.

Parameters:
source - the source string
delimiter - a delimiter string
Returns:
the tokens

toStringList

public static java.lang.String toStringList(java.lang.Object[] items,
                                            java.lang.String delimiter)
Creates a string consisting of the array's items separated by the given delimiter.

Parameters:
items - the string items
delimiter - a delimiter string
Returns:
the concatenated string

toStringList

public static java.lang.String toStringList(java.util.Collection items,
                                            java.lang.String delimiter)
Creates a string consisting of the array's items separated by the given delimiter.

Parameters:
items - the string items
delimiter - a delimiter string
Returns:
the concatenated string

strtok

public static java.lang.String[] strtok(java.lang.String source,
                                        java.lang.String delimiter,
                                        boolean trim)
Determines all tokens of a source string that are delimited by characters of a delimiter string, optionally trims the tokens.

Parameters:
source - the source string
delimiter - a delimiter string
trim - true if tokens shall be trimmed
Returns:
the tokens

strtok

public static java.lang.String[] strtok(java.lang.String source)
Determines all tokens of a source string that are delimited by whitespaces.

Parameters:
source - the source string
Returns:
the tokens

substitute

public static java.lang.String substitute(java.lang.String source,
                                          java.lang.String substitute,
                                          java.lang.String replacement)
Replaces one occurrence of a string to be substituted in a source string by a replacing string.

Parameters:
source - the source string
substitute - the string to be substituted
replacement - the replacing string
Returns:
entire string with replaced occurrence

substituteAll

public static java.lang.String substituteAll(java.lang.String source,
                                             java.lang.String substitute,
                                             java.lang.String replacement)
Replaces all occurrences of a string to be substituted in a source string by a replacing string.

Parameters:
source - the source string
substitute - the string to be substituted
replacement - the replacing string
Returns:
entire string with replaced occurrences

extract

public static java.lang.String extract(java.lang.String source,
                                       java.lang.String leftMark,
                                       java.lang.String rightMark)
Extracts a substring from a source string between a left and a right delimiter string.

Parameters:
source - the source string
leftMark - left delimiter string
rightMark - right delimiter string
Returns:
the subtoken

extractAll

public static java.lang.String[] extractAll(java.lang.String source,
                                            java.lang.String leftMark,
                                            java.lang.String rightMark)
Extracts a substring from a source string between a left and a right delimiter string.

Parameters:
source - the source string
leftMark - the left delimiter string
rightMark - the right delimiter string
Returns:
the subtoken

concat

public static java.lang.String concat(java.lang.String source)
Concatenates a paragraph of text into a single line by removing line ends and unnecessary blanks (i.e. blanks at the end and the beginning of each line).

Parameters:
source - the source string
Returns:
the concatenated paragraph

deleteAll

public static java.lang.String deleteAll(java.lang.String source,
                                         java.lang.String delete)
Deletes all occurrences of a substring from a source string.

Parameters:
source - the source string.
delete - string to be deleted
Returns:
the cleaned up string

fractionDigits

public static int fractionDigits(java.lang.String num)
Counts the digits of the fraction of a number's string representation of a source string.

Parameters:
num - string representation of a number
Returns:
the fractional count

deleteBetweenAll

public static java.lang.String deleteBetweenAll(java.lang.String source,
                                                java.lang.String leftMark,
                                                java.lang.String rightMark)
Deletes all occurrences of a substring between a left delimiter and a right delimiter string. The delimiter strings will be removed also.

Parameters:
source - the source string
leftMark - the left delimiter string
rightMark - the right delimiter string
Returns:
the cleaned up string

subtoken

public static java.lang.String subtoken(java.lang.String source,
                                        int index,
                                        java.lang.String delimiter)
Extracts a substring from a source string beginning at a specified position and ending with a specific delimiter.

Parameters:
source - the source string
index - the index to start with
delimiter - delimiting string of the subtoken
Returns:
the subtoken

strtrunc

public static java.lang.String strtrunc(java.lang.String source,
                                        int length)
Truncates a string if it exceeds a certain length.

Parameters:
source - the source string
length - the maximum length
Returns:
the truncated string

strtrunc

public static java.lang.String strtrunc(java.lang.String source,
                                        int length,
                                        java.lang.String cont)
Truncates a string if it exceeds a certain length and appends a given continuation string instead.

Parameters:
source - the source string
length - the maximum length
cont - the continuation to be appended
Returns:
the truncated string

trimZero

public static java.lang.String trimZero(java.lang.String source)
Removes all leading non-digits and zeros from a string.

Parameters:
source - the source string
Returns:
the trimmed string

fill

public static java.lang.String fill(char character,
                                    int length)
Constructs a string with the specified length that only consists of the given character.

Parameters:
character - the character
length - number of characters
Returns:
the constructed string

strbreak

public static java.lang.String strbreak(java.lang.String source,
                                        java.lang.String insert,
                                        int length)
Breaks up a string into smaller pieces of specified length appended by the given string.

Parameters:
source - the source string
insert - the string to insert
length - the maximum line length
Returns:
a new string containing newlines

strnewline

public static java.lang.String strnewline(java.lang.String source,
                                          int length)
Breaks up a string into smaller pieces of specified length appended by a newline.

Parameters:
source - the source string
length - maximum line length
Returns:
a new string containing newlines

escapeChar

public static java.lang.String escapeChar(java.lang.String string)
Replaces in the given string all meta character sequences such as \n, \t etc. by its corresponding character.

Parameters:
string - string containing meta character sequences
Returns:
the processed string

toBoolean

public static boolean toBoolean(java.lang.String string,
                                boolean def)
Converts a String to a boolean value. The given default is returned in case of a an invalid boolean representation.

Parameters:
string - the number string
def - the default value
Returns:
a boolean value converted from the string

toInt

public static int toInt(java.lang.String string,
                        int def)
Converts a String to an int value. The given default is returned in case of a NumberFormatException.

Parameters:
string - the number string
def - the default value
Returns:
an int value converted from the string

toFloat

public static float toFloat(java.lang.String string,
                            float def)
Converts a String to a float value. The given default is returned in case of a NumberFormatException.

Parameters:
string - the number string
def - the default value
Returns:
a float value converted from the string

toLong

public static long toLong(java.lang.String string,
                          long def)
Converts a String to a long value. The given default is returned in case of a NumberFormatException.

Parameters:
string - the number string
def - the default value
Returns:
a long value converted from the string

toDouble

public static double toDouble(java.lang.String string,
                              double def)
Converts a String to a double value. The given default is returned in case of a NumberFormatException.

Parameters:
string - the number string
def - the default value
Returns:
a double value converted from the string

getDefault

public static java.lang.String getDefault(java.lang.String string,
                                          java.lang.String def)
If the given string is null the default value provided is returned.

Parameters:
string - the number string
def - the default value
Returns:
a string

toColor

public static java.awt.Color toColor(java.lang.String colorName)
Converts a color name or description like #FFCC00 into a color object.

Parameters:
colorName - the color name
Returns:
a color object, which is black by default or error

toCRC

public static long toCRC(java.lang.String string)
Creates a crc checksum from a String.

Parameters:
string - the string
Returns:
long value representing the checksum

getClassName

public static java.lang.String getClassName(java.lang.Class clazz)
Extracts the base class name (i.e. class name w/o package prefix) of the given class.

Parameters:
clazz - the class
Returns:
the base class name with package prefix stripped

isIdentifier

public static boolean isIdentifier(java.lang.String token)
Checks whether a given string follows the rules for identifiers or not.

Parameters:
token - the token to be analyzed
Returns:
true if the given token complies to identifier rules otherwise false

isHexDigit

public static boolean isHexDigit(char character)
Checks whether a given char can be part of a hex string.

Parameters:
character - the character to be analyzed
Returns:
true if the given char can be part of a hex string false otherwise

strapp

public static java.lang.String strapp(java.lang.String source,
                                      java.lang.String append)
Append a string to the source string if it has not been appended already.

Parameters:
source - the source string
append - string to be appended
Returns:
the concatenated string

toProperties

public static java.util.Properties toProperties(java.lang.String itemString,
                                                java.lang.String delimiter)
Creates a Properties object from a key-value pair string which looks something like item1 = value1; item2 = value2; ...

Parameters:
itemString - a string containing key-value pairs
delimiter - delimiter string
Returns:
the initialized Properties object

escapeXmlAttributeValue

public static java.lang.String escapeXmlAttributeValue(java.lang.String value)
Escapes all xml meta-characters in the given string by the corresponding entity char sequences. The resulting string can be safely used as an attribute value of an xml tag.

Parameters:
value - the value string
Returns:
a string where meta-characters have been replaced / escaped


Copyright © 2008-2010. All Rights Reserved.