Class TextUtils
java.lang.Object
nl.colorize.util.TextUtils
Miscellaneous utility and convenience versions for working with text,
including regular expressions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.google.common.base.Joinerstatic final com.google.common.base.Splitter -
Method Summary
Modifier and TypeMethodDescriptionstatic StringaddLeading(String str, String prefix) Adds the specified prefix string to the start of another string, but only if it is not already there.static StringaddTrailing(String str, String suffix) Adds the specified suffix string to the end of another string, but only if it is not already there.static booleancontains(String str, Collection<String> alternatives) static intcountOccurrences(String haystack, String needle) Returns the number of occurrences of the stringneedlewithin the stringhaystack.static booleanendsWith(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 voidRuns a regular expression on all lines within the specified string, then invokes a callback function for every match.static StringnumberFormat(float n, int decimals) Formats a floating point number with the specified number of decimals.static StringremoveLeading(String str, String leading) static StringremoveLeadingAndTrailing(String str, String leading, String trailing) static StringremoveTrailing(String str, String trailing) static booleanstartsWith(String str, Collection<String> alternatives) static StringtimeFormat(float seconds, boolean includeMilliseconds) Formats elapsed time, represented by the number of seconds.static StringtimeFormat(long milliseconds, boolean includeMilliseconds) Formats elapsed time, represented by the number of milliseconds.static StringtoTitleCase(String str) Returns the stringstrformatted 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
-
countOccurrences
Returns the number of occurrences of the stringneedlewithin the stringhaystack.- Throws:
IllegalArgumentException- ifneedleis an empty string.
-
addLeading
-
addTrailing
-
removeLeading
-
removeTrailing
-
removeLeadingAndTrailing
-
startsWith
-
endsWith
-
contains
-
matchAll
-
matchAll
-
matchFirst
-
matchFirst
-
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
-
matchLines
-
matchLines
-
numberFormat
Formats a floating point number with the specified number of decimals. This method is a convenience version forNumberFormat. It uses theen_USlocale, 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 ofincludeMillisecondsis 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 ofincludeMillisecondsis set to true, the number of milliseconds will be appended to the result.
-