at.tuwien.ifs.somtoolbox.util
Class StringUtils

java.lang.Object
  extended by at.tuwien.ifs.somtoolbox.util.StringUtils

public class StringUtils
extends java.lang.Object

This class provides utility methods for String manipulation.

Version:
$Id: StringUtils.java 4183 2011-02-18 12:53:50Z mayer $
Author:
Rudolf Mayer

Field Summary
private static java.util.HashMap<java.lang.String,java.text.DecimalFormat> decimalFormats
           
private static java.lang.String DEFAULT_ARRAY_SEPARATOR
           
private static java.lang.String DEFAULT_ELIPSIS
           
static java.lang.String REGEX_SPACE_OR_TAB
           
static java.lang.String[][] STRING_URLENCODE_REPLACEMENTS
           
 
Constructor Summary
StringUtils()
           
 
Method Summary
static java.lang.String appendExtension(java.lang.String fileName, java.lang.String extension)
           
static java.lang.String appendOrReplaceExtension(java.lang.String fileName, java.lang.String oldExtension, java.lang.String newExtension)
           
static java.lang.String beautify(java.lang.String source)
           
static java.lang.String beautifyForHTML(java.lang.String source)
           
static java.lang.String[] concat(java.lang.String[] array1, java.lang.String[] array2)
           
static boolean containsAny(java.lang.String s, java.lang.String... options)
           
static java.lang.String defaultIfBlank(java.lang.String s, java.lang.String defaultValue)
          Works analogous to StringUtils.defaultIfEmpty(String, String), but using StringUtils.isBlank(String) instead of StringUtils.isEmpty(String)
static boolean endsWithAny(java.lang.String s, java.lang.String... options)
           
static java.lang.String ensureExtension(java.lang.String fileName, java.lang.String extension)
           
static boolean equals(java.lang.Object o, java.lang.String s)
           
static boolean equalsAny(java.lang.Object o, java.lang.String... options)
           
static boolean equalsAny(java.lang.String s, java.lang.Object... options)
          Checks whether the given String equals any of the given options when calling Object.toString() on them
static boolean equalsAny(java.lang.String s, java.lang.String... options)
          Checks whether the given String equals any of the given options.
static boolean equalsAnyIgnoreCase(java.lang.String s, java.lang.String... options)
          Checks whether the given String equals any of the given options, ignoring case
static java.lang.String escapeClassNameForWeka(java.lang.String label)
           
static java.lang.String escapeForWeka(java.lang.String label)
           
static java.lang.String escapeString(java.lang.String s)
           
static java.lang.String format(double number, int fractionDigits)
          Formats a double value with the given number of fractionDigits.
static java.lang.String format(double number, int fractionDigits, boolean withZeros)
          Formats a double value with the given number of fractionDigits.
static java.lang.String format(double number, int fractionDigits, boolean withZeros, int leadingDigits)
           
static java.lang.String format(int number, int digits)
          Formats a double value with the given number of digits, potentially including leading zeros.
static java.lang.String formatAsPercent(double value, double maxValue, int fractionDigits)
           
static java.lang.String formatBooleanValue(int type, boolean value)
          formats a boolean value according to the given type returns the needed string representation of a boolean value that can be printed to the report.
At the moment, only one transtormation is supported: type: 0: true -> yes, false->no (default)
static java.lang.String formatDouble(double value)
          returns the correct format of a double needed for displaying it in the comparison table this means, that if the value is smaller than 0, the string "-" is returned instead of the value
static java.lang.String formatEndMaxLengthEllipsis(java.lang.String s, int maxLen)
           
static java.lang.String formatEndMaxLengthEllipsis(java.lang.String s, int maxLen, java.lang.String ellipsis)
           
static java.lang.String formatMaxLengthEllipsis(java.lang.String s, int maxLen)
           
static java.lang.String formatMaxLengthEllipsis(java.lang.String s, int maxLen, java.lang.String ellipsis)
           
static java.lang.String formatString(java.lang.String value)
          returns the correct format of the given String needed for displaying it in the comparison table this means, that if the String is null or empty, "-" is returned instead of the String
static java.lang.String getCommonPrefix(java.util.Collection<java.lang.String> c)
           
static java.lang.String getCommonPrefix(java.lang.String[] a)
           
static java.lang.String getCommonPrefix(java.lang.String s1, java.lang.String s2)
          Returns the common starting portion of the two Strings, or an empty String if there is no common part.
static java.lang.String getCommonSuffix(java.util.Collection<java.lang.String> c)
           
static java.lang.String getCommonSuffix(java.lang.String[] a)
           
static java.lang.String getCommonSuffix(java.lang.String s1, java.lang.String s2)
          Returns the common starting portion of the two Strings, or an empty String if there is no common part.
private static java.text.DecimalFormat getDecimalFormat(int digits)
           
static java.text.DecimalFormat getDecimalFormat(int fractionDigits, boolean withZeros, int leadingDigits)
          Returns a DecimalFormat with the given number of fractionDigits, with or without trailing zeros.
static java.lang.String[] getDifferences(java.lang.String[] a)
           
static java.text.DecimalFormat getIntegerFormat(int digits)
          Returns a DecimalFormat intended to formatting integers with the given number of digits, potentially with leading zeros
static java.lang.String getLatexRGBString(int[] rgb)
          creates an representation of a color that can be used in html or css takes an int - array of size 3 and transforms it to a hex String of structure #rrggbb The array must have at least length 3 and must not contain null values.
static int getLongestStringLength(java.lang.Iterable<java.lang.String> c)
           
static int getLongestStringLength(java.lang.String[] c)
           
static java.lang.String getRGBString(int[] rgb)
          creates an representation of a color that can be used in html or css takes an int - array of size 3 and transforms it to a hex String of structure #rrggbb The array must have at least length 3 and must not contain null values.
static java.lang.String getSpaces(int num)
           
static int indexOfStartsWithAny(java.lang.String s, java.lang.String... options)
          Checks whether the given String starts with any of the given options.
static int indexOfStartsWithAny(java.lang.String s, java.lang.String suffix, java.lang.String... options)
          Checks whether the given String starts with any of the given options concatenated by the suffix.
static java.lang.String interleave(double[] d, java.lang.String string)
           
static java.lang.String interleave(java.lang.String[] s, java.lang.String string)
           
static int levenshteinDistance(java.lang.String s, java.lang.String t)
          Calculate the Levenshtein Distance between the two given Strings (String1 and String2).
static void main(java.lang.String[] args)
           
static java.lang.String makeStringEndWithCorrectFileSeparator(java.lang.String path)
          Makes sure that the given String ends with the OS-correct File.separator ('/' on Unix, '\\' on Windows)
static boolean matchesAny(java.lang.String[] regExps, java.lang.String s)
           
static int notEmpty(java.lang.Object o)
           
static java.lang.String pad(double value, int len)
           
static java.lang.String pad(java.lang.String s, int len)
           
static double[] parseDoubles(java.lang.String s)
          parses double values from a comma-separated string
private static double[] parseDoubles(java.lang.String[] parts)
          parses double values from a string array, after trimming of the strings is performed
static double[] parseDoublesAndRanges(java.lang.String s)
           
static java.lang.String printMap(java.util.Map m)
           
static java.lang.String readableBytes(long byteSize)
          Formats byte size in nice format
static java.lang.String repeatString(int num, java.lang.String s)
           
static java.lang.String replaceURLEncode(java.lang.String s)
           
static boolean startsWithAny(java.lang.String s, java.lang.String... options)
          Checks whether the given String starts with any of the given options.
static boolean startsWithAnyIgnoreCase(java.lang.String s, java.lang.String... options)
          Checks whether the given String starts with any of the given options, ignoring the case
static java.lang.String stripQuotes(java.lang.String s)
           
static java.lang.String stripSuffix(java.lang.String sMitSuffix)
          Returns the string until (excluding) the first dot (.)
static java.lang.String toString(java.util.Collection<?> collection)
           
static java.lang.String toString(java.util.Collection<?> collection, java.lang.String start, java.lang.String end)
           
static java.lang.String toString(java.util.Collection<?> collection, java.lang.String start, java.lang.String end, java.lang.String separator)
           
static java.lang.String toString(double[][] data)
           
static java.lang.String toString(double[][][] data)
           
static java.lang.String toString(double[] a, int maxElements)
          Returns a string representation of the contents of the specified array in the same fashion as Arrays.toString(double[]), but limiting the output to the given maxIndices parameter.
static java.lang.String toString(double[] array, java.lang.String start, java.lang.String end)
           
static java.lang.String toString(int[] array, java.lang.String start, java.lang.String end)
           
static java.lang.String toString(java.lang.Object[] array, int maxElements)
           
static java.lang.String toString(java.lang.Object[] array, java.lang.String start, java.lang.String end)
           
static java.lang.String toString(java.lang.Object[] array, java.lang.String start, java.lang.String end, java.lang.String separator)
           
static java.lang.String toString(java.lang.Object[] array, java.lang.String start, java.lang.String end, java.lang.String separator, int maxElements)
           
static java.lang.String toString(java.lang.Object[] array, java.lang.String start, java.lang.String end, java.lang.String separator, java.lang.String encasing)
           
static java.lang.String toString(java.awt.geom.Point2D[] a)
           
static java.lang.String toString(java.awt.geom.Point2D[][] a)
           
static java.lang.String toStringWithPrecision(double[] a, int digits)
          Prints a double[] with the given number of decimal digits.
static java.lang.String[] trim(java.lang.String[] split)
           
static java.lang.String URLencode(java.lang.String url)
          This extra method had to be written because URLEncoder.encode 1) performs an encoding of a slash (/), 2) encodes a space as a +, but should encode it as %20.
static java.lang.String wrap(java.lang.String string, int lineWidth)
           
static java.lang.String wrap(java.lang.String string, int lineWidth, int indent)
           
static java.lang.String wrap(java.lang.String string, int lineWidth, int indent, boolean wordBoundaries)
           
static java.lang.String wrap(java.lang.String string, int lineWidth, java.lang.String indentString)
           
static java.lang.String wrap(java.lang.String string, int lineWidth, java.lang.String indentString, boolean wordBoundaries)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REGEX_SPACE_OR_TAB

public static final java.lang.String REGEX_SPACE_OR_TAB
See Also:
Constant Field Values

DEFAULT_ARRAY_SEPARATOR

private static final java.lang.String DEFAULT_ARRAY_SEPARATOR
See Also:
Constant Field Values

DEFAULT_ELIPSIS

private static final java.lang.String DEFAULT_ELIPSIS
See Also:
Constant Field Values

decimalFormats

private static java.util.HashMap<java.lang.String,java.text.DecimalFormat> decimalFormats

STRING_URLENCODE_REPLACEMENTS

public static final java.lang.String[][] STRING_URLENCODE_REPLACEMENTS
Constructor Detail

StringUtils

public StringUtils()
Method Detail

getDecimalFormat

public static java.text.DecimalFormat getDecimalFormat(int fractionDigits,
                                                       boolean withZeros,
                                                       int leadingDigits)
Returns a DecimalFormat with the given number of fractionDigits, with or without trailing zeros.


getIntegerFormat

public static java.text.DecimalFormat getIntegerFormat(int digits)
Returns a DecimalFormat intended to formatting integers with the given number of digits, potentially with leading zeros


format

public static java.lang.String format(double number,
                                      int fractionDigits)
Formats a double value with the given number of fractionDigits. uses format(double, int, boolean) with no trailing zeros.


formatAsPercent

public static java.lang.String formatAsPercent(double value,
                                               double maxValue,
                                               int fractionDigits)

format

public static java.lang.String format(double number,
                                      int fractionDigits,
                                      boolean withZeros)
Formats a double value with the given number of fractionDigits.

Parameters:
withZeros - indicates whether there should be trailing zeros to fill up all fraction digits

format

public static java.lang.String format(double number,
                                      int fractionDigits,
                                      boolean withZeros,
                                      int leadingDigits)

format

public static java.lang.String format(int number,
                                      int digits)
Formats a double value with the given number of digits, potentially including leading zeros.


stripSuffix

public static java.lang.String stripSuffix(java.lang.String sMitSuffix)
Returns the string until (excluding) the first dot (.)

Returns:
filename without suffices

stripQuotes

public static java.lang.String stripQuotes(java.lang.String s)

makeStringEndWithCorrectFileSeparator

public static java.lang.String makeStringEndWithCorrectFileSeparator(java.lang.String path)
Makes sure that the given String ends with the OS-correct File.separator ('/' on Unix, '\\' on Windows)


readableBytes

public static java.lang.String readableBytes(long byteSize)
Formats byte size in nice format

Parameters:
byteSize - the size in bytes to format
Returns:
Formatted number of bytes (eg: empty, 15 B, 12kB, 821 kB, 3 MB...)

escapeString

public static java.lang.String escapeString(java.lang.String s)

toString

public static java.lang.String toString(java.util.Collection<?> collection)

toString

public static java.lang.String toString(java.util.Collection<?> collection,
                                        java.lang.String start,
                                        java.lang.String end)

toString

public static java.lang.String toString(java.util.Collection<?> collection,
                                        java.lang.String start,
                                        java.lang.String end,
                                        java.lang.String separator)

toString

public static java.lang.String toString(java.lang.Object[] array,
                                        java.lang.String start,
                                        java.lang.String end)

toString

public static java.lang.String toString(java.lang.Object[] array,
                                        java.lang.String start,
                                        java.lang.String end,
                                        java.lang.String separator)

toString

public static java.lang.String toString(java.lang.Object[] array,
                                        java.lang.String start,
                                        java.lang.String end,
                                        java.lang.String separator,
                                        java.lang.String encasing)

toString

public static java.lang.String toString(java.lang.Object[] array,
                                        int maxElements)

toString

public static java.lang.String toString(java.lang.Object[] array,
                                        java.lang.String start,
                                        java.lang.String end,
                                        java.lang.String separator,
                                        int maxElements)

toString

public static java.lang.String toString(int[] array,
                                        java.lang.String start,
                                        java.lang.String end)

toString

public static java.lang.String toString(double[] array,
                                        java.lang.String start,
                                        java.lang.String end)

beautifyForHTML

public static java.lang.String beautifyForHTML(java.lang.String source)

beautify

public static java.lang.String beautify(java.lang.String source)

equals

public static boolean equals(java.lang.Object o,
                             java.lang.String s)

equalsAny

public static boolean equalsAny(java.lang.String s,
                                java.lang.String... options)
Checks whether the given String equals any of the given options.


equalsAny

public static boolean equalsAny(java.lang.String s,
                                java.lang.Object... options)
Checks whether the given String equals any of the given options when calling Object.toString() on them


equalsAnyIgnoreCase

public static boolean equalsAnyIgnoreCase(java.lang.String s,
                                          java.lang.String... options)
Checks whether the given String equals any of the given options, ignoring case


equalsAny

public static boolean equalsAny(java.lang.Object o,
                                java.lang.String... options)

startsWithAny

public static boolean startsWithAny(java.lang.String s,
                                    java.lang.String... options)
Checks whether the given String starts with any of the given options.


indexOfStartsWithAny

public static int indexOfStartsWithAny(java.lang.String s,
                                       java.lang.String... options)
Checks whether the given String starts with any of the given options.


indexOfStartsWithAny

public static int indexOfStartsWithAny(java.lang.String s,
                                       java.lang.String suffix,
                                       java.lang.String... options)
Checks whether the given String starts with any of the given options concatenated by the suffix.


endsWithAny

public static boolean endsWithAny(java.lang.String s,
                                  java.lang.String... options)

containsAny

public static boolean containsAny(java.lang.String s,
                                  java.lang.String... options)

matchesAny

public static boolean matchesAny(java.lang.String[] regExps,
                                 java.lang.String s)

startsWithAnyIgnoreCase

public static boolean startsWithAnyIgnoreCase(java.lang.String s,
                                              java.lang.String... options)
Checks whether the given String starts with any of the given options, ignoring the case


toStringWithPrecision

public static java.lang.String toStringWithPrecision(double[] a,
                                                     int digits)
Prints a double[] with the given number of decimal digits.


toString

public static java.lang.String toString(double[] a,
                                        int maxElements)
Returns a string representation of the contents of the specified array in the same fashion as Arrays.toString(double[]), but limiting the output to the given maxIndices parameter.


toString

public static java.lang.String toString(double[][] data)

toString

public static java.lang.String toString(double[][][] data)

getDecimalFormat

private static java.text.DecimalFormat getDecimalFormat(int digits)

getSpaces

public static java.lang.String getSpaces(int num)

repeatString

public static java.lang.String repeatString(int num,
                                            java.lang.String s)

getLongestStringLength

public static int getLongestStringLength(java.lang.Iterable<java.lang.String> c)

getLongestStringLength

public static int getLongestStringLength(java.lang.String[] c)

formatMaxLengthEllipsis

public static java.lang.String formatMaxLengthEllipsis(java.lang.String s,
                                                       int maxLen)

formatMaxLengthEllipsis

public static java.lang.String formatMaxLengthEllipsis(java.lang.String s,
                                                       int maxLen,
                                                       java.lang.String ellipsis)

formatEndMaxLengthEllipsis

public static java.lang.String formatEndMaxLengthEllipsis(java.lang.String s,
                                                          int maxLen)

formatEndMaxLengthEllipsis

public static java.lang.String formatEndMaxLengthEllipsis(java.lang.String s,
                                                          int maxLen,
                                                          java.lang.String ellipsis)

toString

public static java.lang.String toString(java.awt.geom.Point2D[] a)

toString

public static java.lang.String toString(java.awt.geom.Point2D[][] a)

printMap

public static java.lang.String printMap(java.util.Map m)

getCommonPrefix

public static java.lang.String getCommonPrefix(java.lang.String s1,
                                               java.lang.String s2)
Returns the common starting portion of the two Strings, or an empty String if there is no common part.


getCommonPrefix

public static java.lang.String getCommonPrefix(java.util.Collection<java.lang.String> c)

getCommonPrefix

public static java.lang.String getCommonPrefix(java.lang.String[] a)

getCommonSuffix

public static java.lang.String getCommonSuffix(java.lang.String s1,
                                               java.lang.String s2)
Returns the common starting portion of the two Strings, or an empty String if there is no common part.


getCommonSuffix

public static java.lang.String getCommonSuffix(java.util.Collection<java.lang.String> c)

getCommonSuffix

public static java.lang.String getCommonSuffix(java.lang.String[] a)

getDifferences

public static java.lang.String[] getDifferences(java.lang.String[] a)

levenshteinDistance

public static int levenshteinDistance(java.lang.String s,
                                      java.lang.String t)
Calculate the Levenshtein Distance between the two given Strings (String1 and String2).

Parameters:
s - String1
t - String2
Returns:
the Levenshtein Distance between the two strings
See Also:
http://www.javalobby.org/java/forums/t15908.html

concat

public static java.lang.String[] concat(java.lang.String[] array1,
                                        java.lang.String[] array2)

trim

public static java.lang.String[] trim(java.lang.String[] split)

main

public static void main(java.lang.String[] args)

replaceURLEncode

public static java.lang.String replaceURLEncode(java.lang.String s)

formatBooleanValue

public static java.lang.String formatBooleanValue(int type,
                                                  boolean value)
formats a boolean value according to the given type returns the needed string representation of a boolean value that can be printed to the report.
At the moment, only one transtormation is supported:

Parameters:
type - the type of conversion/strings wished (see above)
value - the boolean value
Returns:
a string that encodes the boolean value in a way that it can be printed

getRGBString

public static java.lang.String getRGBString(int[] rgb)
creates an representation of a color that can be used in html or css takes an int - array of size 3 and transforms it to a hex String of structure #rrggbb The array must have at least length 3 and must not contain null values.

Parameters:
rgb - an array specifying the red, green and blue parts of the color
Returns:
a hex string with structure #rrggbb according to the values in the given array

getLatexRGBString

public static java.lang.String getLatexRGBString(int[] rgb)
creates an representation of a color that can be used in html or css takes an int - array of size 3 and transforms it to a hex String of structure #rrggbb The array must have at least length 3 and must not contain null values.

Parameters:
rgb - an array specifying the red, green and blue parts of the color
Returns:
a hex string with structure #rrggbb according to the values in the given array

formatString

public static java.lang.String formatString(java.lang.String value)
returns the correct format of the given String needed for displaying it in the comparison table this means, that if the String is null or empty, "-" is returned instead of the String

Parameters:
value - the string that shall be formatted
Returns:
the input string or "-" if the input is inadequate

formatDouble

public static java.lang.String formatDouble(double value)
returns the correct format of a double needed for displaying it in the comparison table this means, that if the value is smaller than 0, the string "-" is returned instead of the value

Parameters:
value - a double value for which a String representation is needed
Returns:
a string containing the value of the double up to 2 values behind the ., or "-" if the value is smaller 0

URLencode

public static java.lang.String URLencode(java.lang.String url)
This extra method had to be written because URLEncoder.encode 1) performs an encoding of a slash (/), 2) encodes a space as a +, but should encode it as %20. Note that URLEncoder.encode is originally meant for encoding HTML form data only, not arbitrary URLs. However, no other methods of encoding special characters in URLs has been found (the URI methods did not work).

Parameters:
url - a non-encoded URL
Returns:
encoded URL

ensureExtension

public static java.lang.String ensureExtension(java.lang.String fileName,
                                               java.lang.String extension)

interleave

public static java.lang.String interleave(java.lang.String[] s,
                                          java.lang.String string)

interleave

public static java.lang.String interleave(double[] d,
                                          java.lang.String string)

appendExtension

public static java.lang.String appendExtension(java.lang.String fileName,
                                               java.lang.String extension)

appendOrReplaceExtension

public static java.lang.String appendOrReplaceExtension(java.lang.String fileName,
                                                        java.lang.String oldExtension,
                                                        java.lang.String newExtension)

pad

public static java.lang.String pad(double value,
                                   int len)

pad

public static java.lang.String pad(java.lang.String s,
                                   int len)

wrap

public static java.lang.String wrap(java.lang.String string,
                                    int lineWidth)
Parameters:
string - The String to wrap
lineWidth - the width string is wrapped to
Returns:
the string wrapped to the width of lineWidth

wrap

public static java.lang.String wrap(java.lang.String string,
                                    int lineWidth,
                                    int indent)
Parameters:
string - The String to wrap
lineWidth - the width string is wrapped to
indent - prefix each newly added line with indent spaces
Returns:
the string wrapped to the width of lineWidth

wrap

public static java.lang.String wrap(java.lang.String string,
                                    int lineWidth,
                                    int indent,
                                    boolean wordBoundaries)
Parameters:
string - The String to wrap
lineWidth - the width string is wrapped to
indent - prefix each newly added line with indent spaces
wordBoundaries - only wrap at word boudaries (aka Spaces).
Returns:
the string wrapped to the width of lineWidth

wrap

public static java.lang.String wrap(java.lang.String string,
                                    int lineWidth,
                                    java.lang.String indentString)
Parameters:
string - The String to wrap
lineWidth - the width string is wrapped to
indentString - each newly added line will be prefixed with this string
Returns:
the string wrapped to the width of lineWidth

wrap

public static java.lang.String wrap(java.lang.String string,
                                    int lineWidth,
                                    java.lang.String indentString,
                                    boolean wordBoundaries)
Parameters:
string - The String to wrap
lineWidth - the width string is wrapped to
indentString - each newly added line will be prefixed with this string
wordBoundaries - only wrap at word boudaries (aka Spaces).
Returns:
the string wrapped to the width of lineWidth

parseDoubles

public static double[] parseDoubles(java.lang.String s)
parses double values from a comma-separated string


parseDoublesAndRanges

public static double[] parseDoublesAndRanges(java.lang.String s)

parseDoubles

private static double[] parseDoubles(java.lang.String[] parts)
parses double values from a string array, after trimming of the strings is performed


escapeForWeka

public static java.lang.String escapeForWeka(java.lang.String label)

escapeClassNameForWeka

public static java.lang.String escapeClassNameForWeka(java.lang.String label)

notEmpty

public static int notEmpty(java.lang.Object o)

defaultIfBlank

public static java.lang.String defaultIfBlank(java.lang.String s,
                                              java.lang.String defaultValue)
Works analogous to StringUtils.defaultIfEmpty(String, String), but using StringUtils.isBlank(String) instead of StringUtils.isEmpty(String)