Class Strings

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

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

    • STANDARD_SUFFIX

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

      public static final int CODEPOINT_0
      Assume all normal decimal digits '0' to '9' are a continuous interval starting with 0, in the Basic Plane. ((int)c = codepoint).
      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.
    • toSuperscript

      public static String toSuperscript(String s)
      Convert a string with only decimal digits into a string with superscript digits.
      Parameters:
      s - the string to convert
      Returns:
      the string with all decimal digits replaced by superscript digits.
      Throws:
      NullPointerException - on argument
      ArrayIndexOutOfBoundsException - if input contains other characters.
    • 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:
    • iterator

      public static Iterator<String> iterator(StringTokenizer tokenizer)
      Returns an iterator for the tokens produced by a string tokenizer.
      Parameters:
      tokenizer - a string tokenizer
      Returns:
      an iterator for the tokens produced by tokenizer
      Throws:
      NullPointerException - if tokenizer is null
      Since:
      1.3
      See Also:
    • quote

      public static String quote(String text, String delimiter, Strings.TextFlow escapeStyle)
      Returns a variant of the given text, quoted according to the given escaping style and enclosed in the given delimiter.
      Parameters:
      text - the text to quote
      delimiter - the opening and closing delimiter to wrap the text between
      escapeStyle - a text flow that escapes the text content
      Returns:
      a variant of the given text, quoted according to escapeStyle and enclosed in delimiter
      Throws:
      NullPointerException - if text, delimiter or escapeStyle is null
    • quote

      public static String quote(String text, String openDelimiter, String closeDelimiter, Strings.TextFlow escapeStyle)
      Returns a variant of the given text, quoted according to the given escaping style and enclosed in the given delimiters.
      Parameters:
      text - the text to quote
      openDelimiter - the opening delimiter to prepend to the text
      closeDelimiter - the closing delimiter to append to the text
      escapeStyle - a text flow that escapes the text content
      Returns:
      a variant of the given text, quoted according to escapeStyle and enclosed in openDelimiter and closeDelimiter
      Throws:
      NullPointerException - if text, openDelimiter, closeDelimiter or escapeStyle is null
    • quoteAsJavaString

      public static String quoteAsJavaString(String text)
      Returns a Java string literal representation of the given text.
      Parameters:
      text - the text to quote
      Returns:
      a string that can be parsed as a Java string literal containing text
      Throws:
      NullPointerException - if text is null
    • quoteAsXMLText

      public static String quoteAsXMLText(String text)
      Returns an XML representation of the given text.
      Parameters:
      text - the text to quote
      Returns:
      a string that can be parsed as XML character content denoting text
      Throws:
      NullPointerException - if text is null