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 boolean
contains
(String str, Collection<String> alternatives) 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
(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
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 afterward.- 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. -
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.
-