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 leading)
    • removeTrailing

      public static String removeTrailing(String str, String trailing)
    • removeLeadingAndTrailing

      public static String removeLeadingAndTrailing(String str, String leading, String trailing)
    • 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)
    • matchAll

      public static List<String> matchAll(String input, Pattern regex, int group)
      Returns all matches for a regular expression.
      Parameters:
      group - Adds the specified capture group to the list of results.
    • matchAll

      public static List<String> matchAll(String input, Pattern regex)
      Returns all matches for a regular expression.
    • matchFirst

      public static Optional<String> matchFirst(String input, Pattern regex, int group)
      Returns the first match of a regular expression.
      Parameters:
      group - Adds the specified capture group to the list of results.
    • matchFirst

      public static Optional<String> matchFirst(String input, Pattern regex)
      Returns the first match of a regular expression.
    • 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

      public static void matchLines(String input, Pattern regex, Consumer<List<String>> callback)
      Runs a regular expression on all lines within the specified string, then invokes a callback function for every match.
    • numberFormat

      public static String numberFormat(float 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(float 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.