Class Strings

java.lang.Object
eu.bandm.tools.util.java.Strings

public class Strings extends Object
Utility class for string manipulation.
  • Field Details

    • linefeedPattern

      static final Pattern linefeedPattern
      Regular expression which matches different kinds of line feeds and preceding whitespace. The linefeeds encodings are "\n", "\r", and \r\n. The replacement is "\n".
    • STANDARD_SUFFIX

      public static final String STANDARD_SUFFIX
      Will be appended to a truncated string.
      See Also:
  • Method Details

    • removeQuotes

      public static String removeQuotes(String in)
      Remove the first and the last character in a string. (Assuming that these are open and close quotations marks.)
      Parameters:
      in - the String from which to remove the quote characters.
      Returns:
      the String without the quote characters.
    • normalizeLinefeed

      public static String normalizeLinefeed(String s)
      Replace linefeed according to different encodings, plus maximal preceding whitespace, by one standard Unix encoding. The replaced encodings are "\n", "\r", and \r\n. The replacement is "\n". The preceding whitespace is a maximal sequence of blanks, tabs, and formfeeds.

      (Please note that there are other linefeed encodings, not covered by this method and seldomly used, like IBM mainframe 0x85.)

      Parameters:
      s - the String in which to replace linefeeds and preceding whitespace.
      Returns:
      the String with normalized linefeeds and preceding whitespace.
    • replaceLinefeedByBlank

      public static String replaceLinefeedByBlank(String s)
      Replace linefeed according to different encodings, plus maximal preceding whitespace, by one single space character. The replaced encodings are "\n", "\r", and \r\n. The replacement is " ". The preceding whitespace is a maximal sequnce of blanks, tabs, and formfeeds.

      (Please note that there are other linefeed encodings, not covered by this method and seldomly used, like IBM mainframe 0x85.)

      Parameters:
      s - the String in which to replace linefeeds and preceding whitespace.
      Returns:
      the String with normalized linefeeds and preceding whitespace.
    • prefix

      public static String prefix(String s, int len, String suffix)
      Deliver the prefix of the given string of the given max length. Append the suffix if the input must be truncated. In this case the result (including the suffix) will have the given length.
      Parameters:
      s - the String to truncate.
      len - the maximal length of the result.
      suffix - the String to append to a truncated input.
      Returns:
      the decorated prefix of the give string.
      Throws:
      IllegalArgumentException - if len < 0 or suffix longer than len.
    • prefix

      public static String prefix(String s, int len)
      Deliver the prefix of the given string of the given max length. Append the STANDARD_SUFFIX if the input has been truncated.
      Parameters:
      s - the String to truncate.
      len - the maximal length of the result.
      Returns:
      the decorated prefix of the give string.
      Throws:
      IllegalArgumentException - if len < 0 or suffix longer than len.
      See Also:
    • hash32

      public static String hash32(Object o)
      Delivers easily readable String representation. The hashcode of the argument is printed based with 32 digits, i.e. '0' to 'k'.
      Parameters:
      o - the object to print.
      Returns:
      the String representation.
    • compose

      public static String compose(String prefix, String suffix)
      Compose two Strings with "." as the standard separator.
      Parameters:
      prefix - the first string.
      suffix - the second string.
      Returns:
      the concatenation or one of the Strings unaltered.
      See Also:
      • compose(String,String,String)
    • uncapitalize

      public static String uncapitalize(String s)
      Turns the very first character to lower case. For this, Locale.ROOT is used. All other characters are unaltered.
      Parameters:
      s - the string to uncapitalize.
      Returns:
      the uncapitalized string.
    • capitalize

      public static String capitalize(String s)
      Turns the very first character to upper case. For this, Locale.ROOT is used. All other characters are unaltered.
      Parameters:
      s - the string to capitalize.
      Returns:
      the capitalized string.
    • splitByFirst

      public static String[] splitByFirst(String text)
      Takes the very first character as delimiter for splitting the rest of the string. The last character is ignored if it is the delimiter, so no empty sub-string is recognized at the end of the list. Is somehow equivalent to the xslt code splitbyfirst in doctypes/d2d_gp/libbasic.xslt.
      Parameters:
      text - the string to split.
      Returns:
      the splitted string.
    • iterableToString

      public static <T> String iterableToString(@Opt @Opt String openDelim, Iterable<T> elements, String separator, @Opt @Opt String closeDelim)
      Convert an Iterable into a string representation by applying Object.toString() to all elements and merging the results.
      Type Parameters:
      T - the type of the items to print.
      Parameters:
      openDelim - prefix of the result, if not null.
      elements - the String representation of which shall be concatenated.
      separator - is inserted between two elements.
      closeDelim - suffix of the result, if not null.
      Returns:
      the composed string.
      See Also:
    • iterableToString

      public static <T> String iterableToString(@Opt @Opt String openDelim, Iterable<T> elements, Function<T,String> fun, String separator, @Opt @Opt String closeDelim)
      Convert an Iterable into a string representation by applying the given function to all elements and merging the results.
      Type Parameters:
      T - the type of the items to print.
      Parameters:
      openDelim - prefix of the result, if not null.
      elements - the String representation of which shall be concatenated.
      fun - delivers the String representation of the elements.
      separator - is inserted between two elements.
      closeDelim - suffix of the result, if not null.
      Returns:
      the composed string.
    • collectionToText

      public static <T> String collectionToText(Collection<T> elements, Function<T,String> fun, String separatorNotLast, String separatorLast, String separatorOnly)
      Convert a collection into a human-readable string representation by applying the given function to all elements and merging the results. Is required for printed English language, for results like »a, b, and c« and »a and b«.
      Type Parameters:
      T - the type of the elements.
      Parameters:
      elements - to display.
      fun - to apply.
      separatorNotLast - separator between multiple elements.
      separatorLast - separator between the last two elements.
      separatorOnly - separator between only two elements.
      Returns:
      a human-readable string representation of the collection.
    • editDistance

      public static int editDistance(String s1, String s2)
      Computes the Levenshtein distance of two strings.
      Parameters:
      s1 - one string
      s2 - another string
      Returns:
      the Levenshtein (edit) distance of the two strings
      See Also: