at.tuwien.ifs.somtoolbox.util
Class StringUtils

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

public class StringUtils
extends Object

This class provides utility methods for String manipulation.

Version:
$Id: StringUtils.java 3888 2010-11-02 17:42:53Z frank $
Author:
Rudolf Mayer

Field Summary
private static HashMap<String,DecimalFormat> decimalFormats
           
private static String DEFAULT_ARRAY_SEPARATOR
           
private static String DEFAULT_ELIPSIS
           
static String REGEX_SPACE_OR_TAB
           
static String[][] STRING_URLENCODE_REPLACEMENTS
           
 
Constructor Summary
StringUtils()
           
 
Method Summary
static String appendExtension(String fileName, String extension)
           
static String appendOrReplaceExtension(String fileName, String oldExtension, String newExtension)
           
static String beautifyForHTML(String source)
           
static String[] concat(String[] array1, String[] array2)
           
static boolean containsAny(String s, String... options)
           
static boolean endsWithAny(String s, String... options)
           
static String ensureExtension(String fileName, String extension)
           
static boolean equals(Object o, String s)
           
static boolean equalsAny(Object o, String... options)
           
static boolean equalsAny(String s, Object... options)
          Checks whether the given String equals any of the given options when calling Object.toString() on them
static boolean equalsAny(String s, String... options)
          Checks whether the given String equals any of the given options.
static boolean equalsAnyIgnoreCase(String s, String... options)
          Checks whether the given String equals any of the given options, ignoring case
static String escapeClassNameForWeka(String label)
           
static String escapeForWeka(String label)
           
static String escapeString(String s)
           
static String format(double number, int fractionDigits)
          Formats a double value with the given number of fractionDigits.
static String format(double number, int fractionDigits, boolean withZeros)
          Formats a double value with the given number of fractionDigits.
static String format(double number, int fractionDigits, boolean withZeros, int leadingDigits)
           
static String format(int number, int digits)
          Formats a double value with the given number of digits, potentially including leading zeros.
static String formatAsPercent(double value, double maxValue, int fractionDigits)
           
static 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 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 String formatEndMaxLengthEllipsis(String s, int maxLen)
           
static String formatEndMaxLengthEllipsis(String s, int maxLen, String ellipsis)
           
static String formatMaxLengthEllipsis(String s, int maxLen)
           
static String formatMaxLengthEllipsis(String s, int maxLen, String ellipsis)
           
static String formatString(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 String getCommonPrefix(Collection<String> c)
           
static String getCommonPrefix(String[] a)
           
static String getCommonPrefix(String s1, String s2)
          Returns the common starting portion of the two Strings, or an empty String if there is no common part.
static String getCommonSuffix(Collection<String> c)
           
static String getCommonSuffix(String[] a)
           
static String getCommonSuffix(String s1, String s2)
          Returns the common starting portion of the two Strings, or an empty String if there is no common part.
private static DecimalFormat getDecimalFormat(int digits)
           
static DecimalFormat getDecimalFormat(int fractionDigits, boolean withZeros, int leadingDigits)
          Returns a DecimalFormat with the given number of fractionDigits, with or without trailing zeros.
static String[] getDifferences(String[] a)
           
static DecimalFormat getIntegerFormat(int digits)
          Returns a DecimalFormat intended to formatting integers with the given number of digits, potentially with leading zeros
static 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(Iterable<String> c)
           
static int getLongestStringLength(String[] c)
           
static 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 String getSpaces(int num)
           
static String interleave(double[] d, String string)
           
static String interleave(String[] s, String string)
           
static int levenshteinDistance(String s, String t)
          Calculate the Levenshtein Distance between the two given Strings (String1 and String2).
static void main(String[] args)
           
static String makeStringEndWithCorrectFileSeparator(String path)
          Makes sure that the given String ends with the OS-correct File.separator ('/' on Unix, '\\' on Windows)
static boolean matchesAny(String[] regExps, String s)
           
static String pad(double value, int len)
           
static String pad(String s, int len)
           
static double[] parseDoubles(String s)
          parses double values from a comma-separated string
private static double[] parseDoubles(String[] parts)
          parses double values from a string array, after trimming of the strings is performed
static double[] parseDoublesAndRanges(String s)
           
static String printMap(Map m)
           
static String readableBytes(long byteSize)
          Formats byte size in nice format
static String repeatString(int num, String s)
           
static String replaceURLEncode(String s)
           
static boolean startsWithAny(String s, String... options)
          Checks whether the given String starts with any of the given options.
static boolean startsWithAnyIgnoreCase(String s, String... options)
          Checks whether the given String starts with any of the given options, ignoring the case
static String stripQuotes(String s)
           
static String stripSuffix(String sMitSuffix)
          Returns the string until (excluding) the first dot (.)
static String toString(Collection<?> collection)
           
static String toString(Collection<?> collection, String start, String end)
           
static String toString(Collection<?> collection, String start, String end, String separator)
           
static String toString(double[][] data)
           
static String toString(double[][][] data)
           
static 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 String toString(double[] array, String start, String end)
           
static String toString(int[] array, String start, String end)
           
static String toString(Object[] array, int maxElements)
           
static String toString(Object[] array, String start, String end)
           
static String toString(Object[] array, String start, String end, String separator)
           
static String toString(Object[] array, String start, String end, String separator, int maxElements)
           
static String toString(Object[] array, String start, String end, String separator, String encasing)
           
static String toString(Point2D[] a)
           
static String toString(Point2D[][] a)
           
static String toStringWithPrecision(double[] a, int digits)
          Prints a double[] with the given number of decimal digits.
static String[] trim(String[] split)
           
static String URLencode(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 String wrap(String string, int lineWidth)
           
static String wrap(String string, int lineWidth, int indent)
           
static String wrap(String string, int lineWidth, int indent, boolean wordBoundaries)
           
static String wrap(String string, int lineWidth, String indentString)
           
static String wrap(String string, int lineWidth, 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 String REGEX_SPACE_OR_TAB
See Also:
Constant Field Values

DEFAULT_ARRAY_SEPARATOR

private static final String DEFAULT_ARRAY_SEPARATOR
See Also:
Constant Field Values

DEFAULT_ELIPSIS

private static final String DEFAULT_ELIPSIS
See Also:
Constant Field Values

decimalFormats

private static HashMap<String,DecimalFormat> decimalFormats

STRING_URLENCODE_REPLACEMENTS

public static final String[][] STRING_URLENCODE_REPLACEMENTS
Constructor Detail

StringUtils

public StringUtils()
Method Detail

getDecimalFormat

public static 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 DecimalFormat getIntegerFormat(int digits)
Returns a DecimalFormat intended to formatting integers with the given number of digits, potentially with leading zeros


format

public static 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 String formatAsPercent(double value,
                                     double maxValue,
                                     int fractionDigits)

format

public static 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 String format(double number,
                            int fractionDigits,
                            boolean withZeros,
                            int leadingDigits)

format

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


stripSuffix

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

Returns:
filename without suffices

stripQuotes

public static String stripQuotes(String s)

makeStringEndWithCorrectFileSeparator

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


readableBytes

public static 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 String escapeString(String s)

toString

public static String toString(Collection<?> collection)

toString

public static String toString(Collection<?> collection,
                              String start,
                              String end)

toString

public static String toString(Collection<?> collection,
                              String start,
                              String end,
                              String separator)

toString

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

toString

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

toString

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

toString

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

toString

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

toString

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

toString

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

beautifyForHTML

public static String beautifyForHTML(String source)

equals

public static boolean equals(Object o,
                             String s)

equalsAny

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


equalsAny

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


equalsAnyIgnoreCase

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


equalsAny

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

startsWithAny

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


endsWithAny

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

containsAny

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

matchesAny

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

startsWithAnyIgnoreCase

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


toStringWithPrecision

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


toString

public static 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 String toString(double[][] data)

toString

public static String toString(double[][][] data)

getDecimalFormat

private static DecimalFormat getDecimalFormat(int digits)

getSpaces

public static String getSpaces(int num)

repeatString

public static String repeatString(int num,
                                  String s)

getLongestStringLength

public static int getLongestStringLength(Iterable<String> c)

getLongestStringLength

public static int getLongestStringLength(String[] c)

formatMaxLengthEllipsis

public static String formatMaxLengthEllipsis(String s,
                                             int maxLen)

formatMaxLengthEllipsis

public static String formatMaxLengthEllipsis(String s,
                                             int maxLen,
                                             String ellipsis)

formatEndMaxLengthEllipsis

public static String formatEndMaxLengthEllipsis(String s,
                                                int maxLen)

formatEndMaxLengthEllipsis

public static String formatEndMaxLengthEllipsis(String s,
                                                int maxLen,
                                                String ellipsis)

toString

public static String toString(Point2D[] a)

toString

public static String toString(Point2D[][] a)

printMap

public static String printMap(Map m)

getCommonPrefix

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


getCommonPrefix

public static String getCommonPrefix(Collection<String> c)

getCommonPrefix

public static String getCommonPrefix(String[] a)

getCommonSuffix

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


getCommonSuffix

public static String getCommonSuffix(Collection<String> c)

getCommonSuffix

public static String getCommonSuffix(String[] a)

getDifferences

public static String[] getDifferences(String[] a)

levenshteinDistance

public static int levenshteinDistance(String s,
                                      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 String[] concat(String[] array1,
                              String[] array2)

trim

public static String[] trim(String[] split)

main

public static void main(String[] args)

replaceURLEncode

public static String replaceURLEncode(String s)

formatBooleanValue

public static 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 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 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 String formatString(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 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 String URLencode(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 String ensureExtension(String fileName,
                                     String extension)

interleave

public static String interleave(String[] s,
                                String string)

interleave

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

appendExtension

public static String appendExtension(String fileName,
                                     String extension)

appendOrReplaceExtension

public static String appendOrReplaceExtension(String fileName,
                                              String oldExtension,
                                              String newExtension)

pad

public static String pad(double value,
                         int len)

pad

public static String pad(String s,
                         int len)

wrap

public static String wrap(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 String wrap(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 String wrap(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 String wrap(String string,
                          int lineWidth,
                          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 String wrap(String string,
                          int lineWidth,
                          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(String s)
parses double values from a comma-separated string


parseDoublesAndRanges

public static double[] parseDoublesAndRanges(String s)

parseDoubles

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


escapeForWeka

public static String escapeForWeka(String label)

escapeClassNameForWeka

public static String escapeClassNameForWeka(String label)