Class TextUtils

java.lang.Object
nl.colorize.util.TextUtils

public final class TextUtils extends Object
Miscellaneous utility and convenience versions for working with text, including regular expressions.
  • Field Details

    • LINE_SPLITTER

      public static final com.google.common.base.Splitter LINE_SPLITTER
    • LINE_JOINER

      public static final com.google.common.base.Joiner LINE_JOINER
  • Method Details

    • toTitleCase

      public static String toTitleCase(String str)
      Returns the string str formatted in "title format". This will make the first letter of each word uppercase, and the rest of the word lowercase. Both whitespace characters and underscores are considered word separators.
    • countOccurrences

      public static int countOccurrences(String haystack, String needle)
      Returns the number of occurrences of the string needle within the string haystack.
      Throws:
      IllegalArgumentException - if needle is an empty string.
    • addLeading

      public static String addLeading(String str, String prefix)
      Adds the specified prefix string to the start of another string, but only if it is not already there.
    • addTrailing

      public static String addTrailing(String str, String suffix)
      Adds the specified suffix string to the end of another string, but only if it is not already there.
    • removeLeading

      public static String removeLeading(String str, String fragment)
      Removes the specified fragment from the start of str.
      Throws:
      IllegalArgumentException - if fragment is empty.
    • removeTrailing

      public static String removeTrailing(String str, String fragment)
      Removes the specified fragment from the end of str.
      Throws:
      IllegalArgumentException - if fragment is empty.
    • removeSurrounding

      public static String removeSurrounding(String str, String fragment)
      Removes the specified fragment from both the start and the end of str.
      Throws:
      IllegalArgumentException - if fragment is empty.
    • removeSurrounding

      public static String removeSurrounding(String str, String leading, String trailing)
      Removes the specified fragments from the start and the end of str.
      Throws:
      IllegalArgumentException - if one of the fragments is empty.
    • startsWith

      public static boolean startsWith(String str, Collection<String> alternatives)
    • endsWith

      public static boolean endsWith(String str, Collection<String> alternatives)
    • contains

      public static boolean contains(String str, Collection<String> alternatives)
    • match

      @Deprecated public static List<MatchResult> match(String input, Pattern regex)
      Deprecated.
      This method is made redundant by Matcher.results(), although that is not yet supported by TeaVM. Avoid using this method if your application does not need to support TeaVM.
      Returns a list containing all matches for a regular expression, with each match being represented by a MatchResult.
    • matchAll

      public static List<String> matchAll(String input, Pattern regex, int group)
      Returns a list containing all matches for a regular expression, with each match being represented by the match group with the specified index.
    • matchAll

      public static List<String> matchAll(String input, Pattern regex)
      Returns a list containing all matches for a regular expression, with each match being represented by the entire matched text.
    • matchFirst

      public static Optional<String> matchFirst(String input, Pattern regex, int group)
      Returns the first match of a regular expression, with the match being represented by the match group with the specified index.
    • matchFirst

      public static Optional<String> matchFirst(String input, Pattern regex)
      Returns the first match of a regular expression, with the match being represented by the entire matched text.
    • matchLines

      public static List<String> matchLines(Reader input, Pattern regex, int group) throws IOException
      Reads all lines, and returns only the lines that match a regular expression. The reader is closed afterward.
      Parameters:
      group - Adds the specified capture group to the list of results.
      Throws:
      IOException - if an I/O error occurs while reading.
    • matchLines

      public static List<String> matchLines(Reader input, Pattern regex) throws IOException
      Reads all lines, and returns only the lines that match a regular expression. The reader is closed afterward.
      Throws:
      IOException - if an I/O error occurs while reading.
    • matchLines

      public static List<String> matchLines(String input, Pattern regex, int group)
      Reads all lines in a string, and returns only the lines that match a regular expression.
      Parameters:
      group - Adds the specified capture group to the list of results.
    • matchLines

      public static List<String> matchLines(String input, Pattern regex)
      Reads all lines in a string, and returns only the lines that match a regular expression.
    • matchLines

      @Deprecated public static void matchLines(String input, Pattern regex, Consumer<List<String>> callback)
      Deprecated.
      Prefer using matchLines(List, Pattern) to have more explicit control over splitting the text into lines.
      Runs a regular expression on all lines within the specified string, then invokes a callback function for every match.
    • matchLines

      public static List<MatchResult> matchLines(List<String> lines, Pattern regex)
      Returns a list containing all lines matching a regular expression, with each match being represented by a MatchResult.
    • limit

      public static String limit(String str, int maxLength)
      Limits the length of a string to the specified value, using ellipses (...) to indicate the string has been shortened.
    • splitChunks

      public static List<List<String>> splitChunks(List<String> lines, Predicate<String> marker)
      Groups a list of strings into "chunks" delimited by line matching the specified predicate. This is conceptually similar to Splitter, but operating on entire lines rather than characters. Also, unlike Splitter, the marker line is included in the resulting chunk.
    • numberFormat

      public static String numberFormat(double n, int decimals)
      Formats a floating point number with the specified number of decimals. This method is a convenience version for NumberFormat. It uses the en_US locale, meaning that it will use the format "1,000.5".
    • timeFormat

      public static String timeFormat(double seconds, boolean includeMilliseconds)
      Formats elapsed time, represented by the number of seconds. The granularity of the returned value depends on how much time has elapsed. For example, 3 seconds will produce “0:03”, while an hour will produce “1:00:00”. If the value of includeMilliseconds is set to true, the number of milliseconds will be appended to the result.
    • timeFormat

      public static String timeFormat(long milliseconds, boolean includeMilliseconds)
      Formats elapsed time, represented by the number of milliseconds. The granularity of the returned value depends on how much time has elapsed. For example, 3 seconds will produce “0:03”, while an hour will produce “1:00:00”. If the value of includeMilliseconds is set to true, the number of milliseconds will be appended to the result.
    • autoSortAsc

      public static Comparator<String> autoSortAsc()
      Returns a Comparator that sorts strings in ascending order, based on what is considered a reasonable default for a human-readable list.
      • If a string can be parsed as a number, it will be sorted as such. This helps to avoid confusion when sorting number-strings in lexicographic order.
      • null values are always sorted at the end.
      • Remaining strings are compared in case-insensitive order.
    • autoSortDesc

      public static Comparator<String> autoSortDesc()
      Returns a Comparator that sorts strings in descending order, based on what is considered a reasonable default for a human-readable list.
      • If a string can be parsed as a number, it will be sorted as such. This helps to avoid confusion when sorting number-strings in lexicographic order.
      • null values are always sorted at the end.
      • Remaining strings are compared in case-insensitive order.