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 Comparator<String> Returns aComparatorthat sorts strings in ascending order, based on what is considered a reasonable default for a human-readable list.static Comparator<String> Returns aComparatorthat sorts strings in descending order, based on what is considered a reasonable default for a human-readable list.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) static StringLimits the length of a string to the specified value, using ellipses (...) to indicate the string has been shortened.static List<MatchResult> Deprecated.Returns a list containing all matches for a regular expression, with each match being represented by the entire matched text.Returns a list containing all matches for a regular expression, with each match being represented by the match group with the specified index.matchFirst(String input, Pattern regex) Returns the first match of a regular expression, with the match being represented by the entire matched text.matchFirst(String input, Pattern regex, int group) Returns the first match of a regular expression, with the match being represented by the match group with the specified index.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 voidDeprecated.Prefer usingmatchLines(List, Pattern)to have more explicit control over splitting the text into lines.static List<MatchResult> matchLines(List<String> lines, Pattern regex) Returns a list containing all lines matching a regular expression, with each match being represented by aMatchResult.static StringnumberFormat(double n, int decimals) Formats a floating point number with the specified number of decimals.static StringremoveLeading(String str, String fragment) Removes the specified fragment from the start ofstr.static StringremoveSurrounding(String str, String fragment) Removes the specified fragment from both the start and the end ofstr.static StringremoveSurrounding(String str, String leading, String trailing) Removes the specified fragments from the start and the end ofstr.static StringremoveTrailing(String str, String fragment) Removes the specified fragment from the end ofstr.splitChunks(List<String> lines, Predicate<String> marker) Groups a list of strings into "chunks" delimited by line matching the specified predicate.static booleanstartsWith(String str, Collection<String> alternatives) static StringtimeFormat(double 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
Removes the specified fragment from the start ofstr.- Throws:
IllegalArgumentException- iffragmentis empty.
-
removeTrailing
Removes the specified fragment from the end ofstr.- Throws:
IllegalArgumentException- iffragmentis empty.
-
removeSurrounding
Removes the specified fragment from both the start and the end ofstr.- Throws:
IllegalArgumentException- iffragmentis empty.
-
removeSurrounding
Removes the specified fragments from the start and the end ofstr.- Throws:
IllegalArgumentException- if one of the fragments is empty.
-
startsWith
-
endsWith
-
contains
-
match
Deprecated.This method is made redundant byMatcher.results(), although that is not yet supported by TeaVM. Avoid using this method if your application does not need to support TeaVM.Returns a list containing all matches for a regular expression, with each match being represented by aMatchResult. -
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
@Deprecated public static void matchLines(String input, Pattern regex, Consumer<List<String>> callback) Deprecated.Prefer usingmatchLines(List, Pattern)to have more explicit control over splitting the text into lines.Runs a regular expression on all lines within the specified string, then invokes a callback function for every match. -
matchLines
Returns a list containing all lines matching a regular expression, with each match being represented by aMatchResult. -
limit
-
splitChunks
Groups a list of strings into "chunks" delimited by line matching the specified predicate. This is conceptually similar toSplitter, but operating on entire lines rather than characters. Also, unlikeSplitter, the marker line is included in the resulting chunk. -
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. -
autoSortAsc
Returns aComparatorthat sorts strings in ascending order, based on what is considered a reasonable default for a human-readable list.- If a string can be parsed as a number, it will be sorted as such. This helps to avoid confusion when sorting number-strings in lexicographic order.
nullvalues are always sorted at the end.- Remaining strings are compared in case-insensitive order.
-
autoSortDesc
Returns aComparatorthat sorts strings in descending order, based on what is considered a reasonable default for a human-readable list.- If a string can be parsed as a number, it will be sorted as such. This helps to avoid confusion when sorting number-strings in lexicographic order.
nullvalues are always sorted at the end.- Remaining strings are compared in case-insensitive order.
-
Matcher.results(), although that is not yet supported by TeaVM.