Package nl.colorize.util
Class TextUtils
java.lang.Object
nl.colorize.util.TextUtils
Miscellaneous utility and convenience versions for working with text,
including regular expressions.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final com.google.common.base.Joiner
static final com.google.common.base.Splitter
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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.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.static int
Calculates the Levenshtein distance between two strings.calculateLongestCommonPrefix
(String[] a, String[] b) Returns the longest possible list of strings that is a prefix for botha
andb
.static String
Returns the longest possible string that is a prefix for botha
andb
.calculateLongestCommonPrefix
(List<String> a, List<String> b) Returns the longest possible list of strings that is a prefix for botha
andb
.static float
Calculates the Levenshtein distance between two strings, but returns the result relative to the length of the longest input string.static boolean
contains
(String str, Collection<String> alternatives) static int
countIndent
(String line) Counts the indent for the specified line.static int
countOccurrences
(String haystack, String needle) Returns the number of occurrences of the stringneedle
within the stringhaystack
.static boolean
endsWith
(String str, Collection<String> alternatives) Returns all matches for a regular expression.Returns all matches for a regular expression.matchFirst
(String input, Pattern regex) Returns the first match of a regular expression.matchFirst
(String input, Pattern regex, int group) Returns the first match of a regular expression.matchLines
(File input, Charset charset, Pattern regex) Reads all lines from a file, and returns only the lines that match a regular expression.matchLines
(File input, Charset charset, Pattern regex, int group) Reads all lines from a file, and returns only the lines that match a regular expression.matchLines
(Reader input, Pattern regex) Reads all lines, and returns only the lines that match a regular expression.matchLines
(Reader input, Pattern regex, int group) Reads all lines, and returns only the lines that match a regular expression.matchLines
(String input, Pattern regex) Reads all lines in a string, and returns only the lines that match a regular expression.matchLines
(String input, Pattern regex, int group) Reads all lines in a string, and returns only the lines that match a regular expression.static void
Runs a regular expression on all lines within the specified string, then invokes a callback function for every match.static String
numberFormat
(float n, int decimals) Formats a floating point number with the specified number of decimals.static String
removeBetween
(String input, String from, String to) Removes all text between and includingfrom
andto
.static String
removeLeading
(String str, String leading) static String
removeLeadingAndTrailing
(String str, String leading, String trailing) static String
removeTrailing
(String str, String trailing) static boolean
startsWith
(String str, Collection<String> alternatives) static String
timeFormat
(float seconds, boolean includeMilliseconds) Formats elapsed time, represented by the number of seconds.static String
timeFormat
(long milliseconds, boolean includeMilliseconds) Formats elapsed time, represented by the number of milliseconds.static String
toTitleCase
(String str) Returns the stringstr
formatted in "title format".
-
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
Returns the stringstr
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
Returns the number of occurrences of the stringneedle
within the stringhaystack
.- Throws:
IllegalArgumentException
- ifneedle
is an empty string.
-
addLeading
Adds the specified prefix string to the start of another string, but only if it is not already there. -
addTrailing
Adds the specified suffix string to the end of another string, but only if it is not already there. -
removeLeading
-
removeTrailing
-
removeLeadingAndTrailing
-
startsWith
-
endsWith
-
contains
-
matchAll
Returns all matches for a regular expression.- Parameters:
group
- Adds the specified capture group to the list of results.
-
matchAll
Returns all matches for a regular expression. -
matchFirst
Returns the first match of a regular expression.- Parameters:
group
- Adds the specified capture group to the list of results.
-
matchFirst
Returns the first match of a regular expression. -
matchLines
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
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
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
Reads all lines in a string, and returns only the lines that match a regular expression. -
matchLines
Runs a regular expression on all lines within the specified string, then invokes a callback function for every match. -
removeBetween
Removes all text between and includingfrom
andto
. If the string does not contain both markers this does nothing and returns the original string. -
countIndent
Counts the indent for the specified line. Tabs are counted as 4 spaces. -
calculateLongestCommonPrefix
Returns the longest possible string that is a prefix for botha
andb
. Returns an empty string if there is no common prefix. -
calculateLongestCommonPrefix
Returns the longest possible list of strings that is a prefix for botha
andb
. Returns an empty list if there is no common prefix. -
calculateLongestCommonPrefix
Returns the longest possible list of strings that is a prefix for botha
andb
. Returns an empty list if there is no common prefix. -
calculateLevenshteinDistance
Calculates the Levenshtein distance between two strings. -
calculateRelativeLevenshteinDistance
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
Formats a floating point number with the specified number of decimals. This method is a convenience version forNumberFormat
. It uses theen_US
locale, meaning that it will use the format "1,000.5". -
timeFormat
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 ofincludeMilliseconds
is set to true, the number of milliseconds will be appended to the result. -
timeFormat
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 ofincludeMilliseconds
is set to true, the number of milliseconds will be appended to the result.
-