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 afterwards.
      Throws:
      IOException - if an I/O error occurs while reading.
    • matchLines

      public static List<String> matchLines(File input, Charset charset, Pattern regex, int group) throws IOException
      Reads all lines from a file, and returns only the lines that match a regular expression.
      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(File input, Charset charset, Pattern regex) throws IOException
      Reads all lines from a file, and returns only the lines that match a regular expression.
      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.
    • removeBetween

      public static String removeBetween(String input, String from, String to)
      Removes all text between and including from and to. If the string does not contain both markers this does nothing and returns the original string.
    • countIndent

      public static int countIndent(String line)
      Counts the indent for the specified line. Tabs are counted as 4 spaces.
    • calculateLongestCommonPrefix

      public static String calculateLongestCommonPrefix(String a, String b)
      Returns the longest possible string that is a prefix for both a and b. Returns an empty string if there is no common prefix.
    • calculateLongestCommonPrefix

      public static List<String> calculateLongestCommonPrefix(List<String> a, List<String> b)
      Returns the longest possible list of strings that is a prefix for both a and b. Returns an empty list if there is no common prefix.
    • calculateLongestCommonPrefix

      public static List<String> calculateLongestCommonPrefix(String[] a, String[] b)
      Returns the longest possible list of strings that is a prefix for both a and b. Returns an empty list if there is no common prefix.
    • calculateLevenshteinDistance

      public static int calculateLevenshteinDistance(String a, String b)
      Calculates the Levenshtein distance between two strings.
    • calculateRelativeLevenshteinDistance

      public static float calculateRelativeLevenshteinDistance(String a, String b)
      Calculates the Levenshtein distance between two strings, but returns the result relative to the length of the longest input string. The returned value is therefore a score between 0.0 (strings are equal) and 1.0 (strings are completely different).
    • 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.