public class StringUtils extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DOT
Dot.
|
static java.lang.String |
EMPTY_STRING
The empty string.
|
static java.lang.String |
LINE_FEED
Line feed ("\n"), platform independent.
|
static java.lang.String |
LINE_SEPARATOR
Line separator of the current platform.
|
static java.util.List<java.lang.Character> |
MARKDOWN_ESCAPE_CHARACTERS
Characters that need to be escaped in markdown.
|
static java.lang.String |
SPACE
A space.
|
static char |
SPACE_CHAR
A space character.
|
static java.lang.String |
TAB
A tab character.
|
static java.lang.String |
TWO_SPACES
Two spaces.
|
Constructor and Description |
---|
StringUtils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
addPrefix(java.lang.String string,
java.lang.String separator,
java.lang.String prefix)
Prefixes a string with a prefix and separator if the prefix is not empty.
|
static java.lang.String |
addSuffix(java.lang.String string,
java.lang.String separator,
java.lang.String suffix)
Suffixes a string with a suffix and separator if the suffix is not empty.
|
static java.lang.String |
applyAllReplacements(java.lang.String s,
PairList<java.util.regex.Pattern,java.lang.String> replacements)
Returns the result of applying all pattern replacements in order, each as
often as possible (globally).
|
static java.lang.String |
applyReplacement(java.lang.String s,
java.util.regex.Pattern pattern,
java.lang.String replacement)
Returns the replacement as often as possible.
|
static java.util.List<java.lang.String> |
asStringList(java.util.Collection<?> objects)
Returns a list containing the string representations of the given collection
of objects.
|
static java.lang.String |
bytesToString(byte[] b)
Converts a (UTF-8) byte array to a string.
|
static java.lang.String |
bytesToString(byte[] b,
java.nio.charset.Charset encoding)
Converts a byte array to a string, assuming the given encoding,
unless a byte-order mark included with the bytes implies
that another encoding is actually used.
|
static java.lang.String |
camelCaseToKebabCase(java.lang.String s)
Transforms a string from camel-case to lower-case with hyphens (aka kebab
case).
|
static java.lang.String |
camelCaseToUnderscored(java.lang.String s)
Transforms a string from camel-case to upper-case with underscores.
|
static java.lang.String |
capitalize(java.lang.String string)
Capitalize string.
|
static java.lang.String |
center(java.lang.String string,
int length,
char c)
Create a string of the given length and center the given string within it.
|
static int |
compare(java.lang.String a,
java.lang.String b)
Compares two strings both of which may be
null . |
static java.lang.String |
concat(java.lang.Iterable<?> iterable)
Concatenates all elements of an iterable using the
toString() -method. |
static java.lang.String |
concat(java.lang.Iterable<?> iterable,
java.lang.String separator)
Concatenates all elements of an iterable using the
toString() -method, separating them with the given
separator . |
static java.lang.String |
concat(java.util.Iterator<?> iterator,
java.lang.String separator)
Concatenates all elements of an iterator using the
toString() -method, separating them with the given
separator . |
static java.lang.String |
concat(java.lang.Object[] array)
Concatenates all elements of an array using the
toString()
-method. |
static java.lang.String |
concat(java.lang.Object[] array,
java.lang.String separator)
Concatenates all elements of an array using the
toString()
-method, separating them with the given separator . |
static java.lang.String[] |
concat(java.lang.String[] array1,
java.lang.String[] array2)
Concatenate two string arrays.
|
static java.lang.String |
concatWithEscapeCharacter(java.util.List<java.lang.String> data,
java.lang.String delimiter)
Concatenates the list of string with delimiter and add escape character if
needed.
|
static boolean |
containsAll(java.lang.String s,
java.lang.String... substrings)
Returns whether s contains all the given substrings.
|
static boolean |
containsIgnoreCase(java.lang.String s1,
java.lang.String s2)
Returns whether s1 contains s2 ignoring case
|
static boolean |
containsLetter(java.lang.String s)
Checks if the given string contains at least one letter (checked with
Character.isLetter(char) ). |
static boolean |
containsOneOf(java.lang.String text,
java.lang.Iterable<java.lang.String> strings)
Test if a string contains of the provided strings.
|
static boolean |
containsOneOf(java.lang.String text,
java.lang.String... strings)
Test if a string contains of the provided strings.
|
static int |
countCharacter(java.lang.String content,
char character)
Returns the number of occurrences of the given character in the given string.
|
static int |
countLines(java.lang.String content)
Return number of lines in a string.
|
static int |
countLines(java.lang.String content,
boolean includeTrailingEmptyLine)
Return number of lines in a string.
|
static java.lang.String |
createUniqueName(java.lang.String baseName,
java.util.Set<java.lang.String> usedNames)
Creates a unique name which is not contained in the given set of names.
|
static java.lang.String |
decapitalize(java.lang.String string)
Decapitalize string.
|
static byte[] |
decodeFromHex(java.lang.String s)
Decodes a byte array from a hex string.
|
static java.lang.String |
defaultIfNull(java.lang.String input,
java.lang.String defaultValue)
Returns the input string.
|
static java.lang.String |
defaultIfNullOrEmpty(java.lang.String input,
java.lang.String defaultValue)
Returns the input string.
|
static int |
editDistance(java.lang.String s,
java.lang.String t)
Calculates the edit distance (aka Levenshtein distance) for two strings, i.e.
|
static java.lang.String |
emptyIfNull(java.lang.String input)
Returns the input string.
|
static java.lang.String |
encodeAsHex(byte[] data)
Encodes a byte array as a hex string following the method described here:
http ://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-
string-in-java
|
static boolean |
endsWithIgnoreCase(java.lang.String string,
java.lang.String suffix)
Returns whether the given String ends with the specified suffix ignoring
case.
|
static boolean |
endsWithOneOf(java.lang.String string,
java.lang.String... suffixes)
Test if a string ends with one of the provided suffixes.
|
static java.lang.String |
ensureEndsWith(java.lang.String s,
java.lang.String suffix)
Ensure that the given string ends with the given suffix, i.e.
|
static java.lang.String |
ensureStartsWith(java.lang.String s,
java.lang.String prefix)
Ensure that the given string starts with the given prefix, i.e.
|
static boolean |
equalsOneOf(java.lang.String value,
java.lang.String... strings)
Compares the given value to all given strings.
|
static java.lang.String |
escapeChars(java.lang.String content,
java.util.List<java.lang.Character> chars)
Escapes the given chars in the content.
|
static java.lang.String |
escapeRegexReplacementString(java.lang.String replacement)
Regex replacement methods like
Matcher.appendReplacement(StringBuffer, String) or
String.replaceAll(String, String) treat dollar signs as group
references. |
static java.lang.String |
fillString(int length,
char c)
Build a string with a specified length from a character.
|
static java.util.List<java.lang.String> |
filterBySuffix(java.lang.String suffix,
java.util.Collection<java.lang.String> strings)
Filters the given collection of strings by the given suffix, i.e.
|
static java.lang.String |
flushLeft(java.lang.String string,
int length,
char c)
Create a sting of the given length starting with the provided string.
|
static java.lang.String |
flushRight(java.lang.String string,
int length,
char c)
Create a sting of the given length ending with the provided string.
|
static java.lang.String |
format(double number,
java.text.NumberFormat numberFormat)
Format number with number formatter, if number formatter is
null , this uses String.valueOf(double) . |
static java.lang.String |
format(java.lang.Number number)
Format number
|
static java.lang.String |
formatAsPercentage(java.lang.Number number)
Format as percentage.
|
static java.lang.String |
fromInputStream(java.io.InputStream inputStream)
Converts the given
InputStream to a String with UTF-8 encoding. |
static java.lang.String |
generateString(int length,
int seed)
Generates a pseudo random string with a certain length in a deterministic,
reproducable fashion.
|
static java.lang.String[] |
generateStringArray(int length,
int stringLength,
int seed)
Generates an array of pseudo-random strings in a deterministic, reproducable
fashion.
|
static java.lang.String |
getBeginning(java.lang.String s,
int numberOfChars)
Returns the beginning of the given String, retaining at most numberOfChars
characters.
|
static java.lang.String |
getFirstLine(java.lang.String string)
Returns the first line of a string.
|
static java.lang.String |
getFirstPart(java.lang.String string,
char separator)
Variant of
getFirstPart(String, String) which accepts a single char
as the separator. |
static java.lang.String |
getFirstPart(java.lang.String string,
java.lang.String separator)
Returns the first part of a String whose parts are separated by the given
character.
|
static java.lang.String |
getFirstParts(java.lang.String string,
int partNumber,
char separator)
Returns the first n part of a string, separated by the given character.
|
static java.util.HashMap<java.lang.String,java.lang.String> |
getKeyValuePairs(java.lang.String keyValueString)
Splits a key-value string and stores it in a hash map.
|
static java.lang.String |
getLastPart(java.lang.String string,
char separator)
Variant of
getLastPart(String, String) which accepts a single char
as the separator. |
static java.lang.String |
getLastPart(java.lang.String string,
java.lang.String separator)
Returns the last part of a String whose parts are separated by the given
String.
|
static int |
indexOf(java.lang.String[] array,
java.lang.String string)
Searches the elements of a string array for a string.
|
static int |
indexOfMatch(java.lang.String string,
java.util.regex.Pattern pattern)
Returns the index of the first character in the given string that matches the
pattern.
|
static boolean |
isEditDistanceAtMost1(java.lang.String s,
java.lang.String t)
Returns whether the edit distance as calculated by
editDistance(String, String) , is 0 or 1. |
static boolean |
isEmpty(java.lang.String text)
Checks if a string is empty (after trimming).
|
static boolean |
isInteger(java.lang.String string)
Checks whether the parameter contains only number literals and (optionally)
starts with a '-' char.
|
static java.lang.String |
joinDifferentLastDelimiter(java.util.List<java.lang.String> items,
java.lang.String delimiter,
java.lang.String lastDelimiter)
Joins the elements in the given list with the given delimiter, but with a
different delimiter for the very last element.
|
static java.lang.String |
kebabCaseToCamelCase(java.lang.String name)
Converts a dash-separated name (aka kebab case) to a camel-cased one.
|
static java.lang.String |
longestCommonPrefix(java.lang.Iterable<java.lang.String> strings)
Returns the longest common prefix of the strings in the list or the empty
string if no common prefix exists.
|
static java.lang.String |
longestCommonPrefix(java.lang.String s,
java.lang.String t)
Returns the longest common prefix of s and t
|
static java.lang.String |
longestCommonSuffix(java.lang.String s,
java.lang.String t)
Returns the longest common suffix of s and t
|
static java.util.List<java.lang.String> |
lowercaseList(java.util.Collection<java.lang.String> strings)
Returns a list that contains all entries of the original list as lowercase
strings.
|
static java.lang.String |
normalizeLineSeparatorsPlatformIndependent(java.lang.String string)
Replace all linebreaks in string with a line feed symbol (\n), platform
independent.
|
static java.lang.String |
normalizeLineSeparatorsPlatformSpecific(java.lang.String string)
Replace all linebreaks in string with the platform-specific line separator
(
LINE_SEPARATOR ). |
static java.lang.String |
nullIfEmpty(java.lang.String input)
Returns
null if the input string is empty (after trimming) or
null . |
static java.lang.String |
obtainStackTrace(java.lang.Throwable throwable)
Convert stack trace of a
Throwable to a string. |
static java.lang.String |
pluralize(java.lang.String string,
int count)
Returns the string itself, if count is 1.
|
static java.lang.String |
prefixLines(java.lang.String string,
java.lang.String prefix,
boolean prefixFirstLine)
Prefix all lines of a string.
|
static java.lang.String |
randomString(int length)
Generates a random string with a certain length.
|
static java.lang.String |
randomString(int length,
java.util.Random random)
Performs the actal creation of the random string using the given randomizer.
|
static java.lang.String[] |
randomStringArray(int length,
int stringLength)
Generates an array of random strings.
|
static java.lang.String |
removeAll(java.lang.String string,
java.lang.String... stringsToRemove)
Removes all occurrences of the specified strings from the given string.
|
static java.lang.String |
removeDoubleQuotes(java.lang.String string)
Removes double quotes from beginning and end (if present) and returns the new
string.
|
static java.lang.String |
removeLastPart(java.lang.String string,
char separator)
Returns the beginning of a String, cutting off the last part which is
separated by the given character.
|
static java.lang.String |
removeSingleQuotes(java.lang.String string)
Removes single quotes from beginning and end (if present) and returns the new
string.
|
static java.lang.String |
removeWhitespace(java.lang.String content)
Removes whitespace from a string.
|
static java.lang.String |
removeWhitespaceAtBeginningOfLine(java.lang.String content)
Removes all whitespace at the beginning of each line in the given string.
|
static java.lang.String |
repeat(java.lang.String s,
int times)
Repeats a
String |
static java.lang.String |
replaceFromMap(java.lang.String string,
java.util.Map<java.lang.String,java.lang.String> replacements)
Replaces all occurrences of keys of the given map in the given string with
the associated value in that map.
|
static java.lang.String |
replaceLineBreaks(java.lang.String string,
java.lang.String symbol)
Replace all linebreaks in string by a specified symbol.
|
static java.lang.String |
retainHeadLines(java.lang.String text,
int numberOfLines)
Returns a truncated string that contains only the first x lines of the given
text.
|
static java.lang.String |
reverse(java.lang.String s)
Reverse a string
|
static java.lang.String |
safeToString(java.lang.Object value)
Object.toString() with null check. |
static Pair<java.lang.String,java.lang.String> |
splitAtLast(java.lang.String string,
char separator)
Splits the string at the last occurence of
separator . |
static java.lang.Character[] |
splitChars(java.lang.String s)
Splits the given string into an array of
Character s. |
static java.lang.String[] |
splitLines(java.lang.String content)
Split string in lines.
|
static java.util.List<java.lang.String> |
splitLinesAsList(java.lang.String content)
Split string in lines.
|
static java.util.List<java.lang.String> |
splitLinesAsList(java.lang.String content,
boolean includeTrailingEmptyLine)
Split string in lines.
|
static java.util.List<java.lang.String> |
splitTopLevel(java.lang.String input,
char separator,
char levelStart,
char levelEnd)
Splits a string at every top-level occurrence of the separator character.
|
static java.util.List<java.lang.String> |
splitWithEscapeCharacter(java.lang.String data,
java.lang.String delimiter)
Splits the delimited string with considering escaped delimiters.
|
static boolean |
startsWithIgnoreCase(java.lang.String string,
java.lang.String prefix)
Returns whether the given string starts with the prefix ignoring case, i.e.
|
static boolean |
startsWithOneOf(java.lang.String string,
java.lang.Iterable<java.lang.String> prefixes)
Test if a string starts with one of the provided prefixes.
|
static boolean |
startsWithOneOf(java.lang.String string,
java.lang.String... prefixes)
Test if a string starts with one of the provided prefixes.
|
static byte[] |
stringToBytes(java.lang.String s)
Converts a string to a (UTF-8) byte representation.
|
static java.lang.String |
stripDigits(java.lang.String string)
Strips all digits from the given String.
|
static java.lang.String |
stripPrefix(java.lang.String string,
java.lang.String prefix)
Remove prefix from a string.
|
static java.lang.String |
stripPrefixIgnoreCase(java.lang.String string,
java.lang.String prefix)
Remove prefix from a string.
|
static java.lang.String |
stripSuffix(java.lang.String string,
java.lang.String suffix)
Remove suffix from a string.
|
static java.lang.String |
stripTrailingDigits(java.lang.String string)
Strips all trailing digits from the end of the given String.
|
static java.lang.String |
surroundWith(java.lang.String s,
java.lang.String prefix,
java.lang.String suffix)
Adds a prefix and a suffix to the given string.
|
static java.lang.String |
toFirstUpper(java.lang.String s)
Converts the given String to a String where the first character is in upper
case and all other characters are in lower case
|
static java.io.InputStream |
toInputStream(java.lang.String string)
Converts the given String to an
InputStream with UTF-8 encoding. |
static java.lang.String |
toString(java.util.Map<?,?> map)
Create string representation of a map.
|
static java.lang.String |
toString(java.util.Map<?,?> map,
java.lang.String indent)
Create string representation of a map.
|
static java.lang.String[] |
toStringArray(java.lang.Object[] array)
Converts the given Object array into a String array by invoking toString on
each non-null element.
|
static <T> java.util.List<java.lang.String> |
toStrings(java.util.Collection<T> objects)
Converts the given objects into a string list by invoking
Object.toString() on each non-null element. |
static java.lang.String |
truncate(java.lang.String string,
int length,
java.lang.String suffix)
Truncates the given string (if necessary) by removing characters from the end
and attaching the suffix such that the resulting string has at most length
characters.
|
static java.lang.String |
unEscapeChars(java.lang.String content,
java.util.List<java.lang.Character> chars)
Un-escapes the given chars in the content.
|
static java.lang.String |
wrapLongLines(java.lang.String s,
int maxLineLength)
This method splits the input string into words (delimited by whitespace) and
returns a string whose words are separated by single spaces and whose lines
are not longer than the given length (unless a very long word occurs)).
|
public static final java.lang.String LINE_SEPARATOR
public static final java.lang.String LINE_FEED
public static final java.lang.String EMPTY_STRING
StringUtils.EMPTY_STRING
instead of an empty string
literal (""
) to make it clear that this is intentionally empty.
Using "" may leave the reader in doubt whether this is something unfinished
(e.g. "" used temporally until something meaningful is filled in) or
intentionally the empty String. Making it explicit removes this problem)public static final java.lang.String SPACE
public static final char SPACE_CHAR
public static final java.lang.String TAB
public static final java.lang.String TWO_SPACES
public static final java.lang.String DOT
public static final java.util.List<java.lang.Character> MARKDOWN_ESCAPE_CHARACTERS
escapeChars(String, List)
and
unEscapeChars(String, List)
public StringUtils()
public static java.lang.String center(java.lang.String string, int length, char c)
string
- The input string.length
- The length of the string to be returned.c
- The character to surround the input string with.flushLeft(String, int, char)
,
flushRight(String, int, char)
public static int compare(java.lang.String a, java.lang.String b)
null
. A string which
is null
is always smaller than the other string, except for both
strings being null
.a
- The string which is compared to the second string.b
- The string which is compared to the first string.null
, -1 if only the first
string is null
, and 1 if only the second string is
null
. If both strings are not null
, returns
the result of the usual string comparison.public static java.lang.String concat(java.lang.Iterable<?> iterable)
toString()
-method.iterable
- the iterablepublic static java.lang.String concat(java.lang.Iterable<?> iterable, java.lang.String separator)
toString()
-method, separating them with the given
separator
.iterable
- the iterable containing the stringsseparator
- the separator to place between the strings, may be
null
null
if
iterable was null
. If the iterable is of size 0 the
empty string is returned.public static java.lang.String concat(java.util.Iterator<?> iterator, java.lang.String separator)
toString()
-method, separating them with the given
separator
.iterator
- the Iterator
containing the stringsseparator
- the separator to place between the strings, may be
null
null
if
iterator was null
. If the iterator has no elements the
empty string is returned.public static java.lang.String concat(java.lang.Object[] array)
toString()
-method.array
- the array containing the stringspublic static java.lang.String concat(java.lang.Object[] array, java.lang.String separator)
toString()
-method, separating them with the given separator
.array
- the arrayseparator
- the separator to place between the strings, may be
null
null
if
array was null
. If array is of length 0 the empty string
is returned.public static java.lang.String joinDifferentLastDelimiter(java.util.List<java.lang.String> items, java.lang.String delimiter, java.lang.String lastDelimiter)
This could, for example, be used if you want to create a String "a, b and c" from a list containing "a", "b" and "c".
public static java.lang.String[] concat(java.lang.String[] array1, java.lang.String[] array2)
public static java.lang.String fillString(int length, char c)
length
- The length of the string.c
- The character.public static java.lang.String flushLeft(java.lang.String string, int length, char c)
string
- The input string.length
- The length of the string to be returned.c
- The character to fill the string.flushRight(String, int, char)
,
center(String, int, char)
public static java.lang.String flushRight(java.lang.String string, int length, char c)
string
- The input string.length
- The length of the string to be returned.c
- The character to fill the string.flushLeft(String, int, char)
,
center(String, int, char)
public static java.lang.String format(java.lang.Number number)
public static java.lang.String formatAsPercentage(java.lang.Number number)
public static java.lang.String getFirstLine(java.lang.String string)
public static java.lang.String getFirstParts(java.lang.String string, int partNumber, char separator)
string
- the base stringpartNumber
- number of partsseparator
- the separator characterpublic static java.util.HashMap<java.lang.String,java.lang.String> getKeyValuePairs(java.lang.String keyValueString)
key=value[,key=value]*
null
null
is returned.keyValueString
- with format described abovepublic static java.lang.String getFirstPart(java.lang.String string, java.lang.String separator)
string
- the Stringseparator
- separation characterpublic static java.lang.String getFirstPart(java.lang.String string, char separator)
getFirstPart(String, String)
which accepts a single char
as the separator.getFirstPart(String, String)
public static java.lang.String getLastPart(java.lang.String string, java.lang.String separator)
string
- the Stringseparator
- separation Stringpublic static java.lang.String getLastPart(java.lang.String string, char separator)
getLastPart(String, String)
which accepts a single char
as the separator.getLastPart(String, String)
public static Pair<java.lang.String,java.lang.String> splitAtLast(java.lang.String string, char separator)
separator
. If the
separator does not occur, the second string in the returned pair is empty.
E.g. splitAtLast(foo.bar.baz, '.') gives ("foo.bar", "baz).public static int indexOf(java.lang.String[] array, java.lang.String string)
array
- the array to searchstring
- the search string-1
if string wasn't found.public static boolean isEmpty(java.lang.String text)
text
- the string to check.true
if string is empty or null
,
false
otherwise.public static boolean containsLetter(java.lang.String s)
Character.isLetter(char)
).public static boolean containsIgnoreCase(java.lang.String s1, java.lang.String s2)
public static boolean containsAll(java.lang.String s, java.lang.String... substrings)
public static java.lang.String randomString(int length)
length
- the length of the random stringpublic static java.lang.String randomString(int length, java.util.Random random)
public static java.lang.String[] randomStringArray(int length, int stringLength)
length
- number of stringsstringLength
- length of each stringpublic static java.lang.String generateString(int length, int seed)
length
- the length of the pseudo-random stringseed
- seed value for the random number generator used for the generation
of the pseudo-random string. If the same seed value is used, the
same pseudo-random string is generated.public static java.lang.String[] generateStringArray(int length, int stringLength, int seed)
length
- number of stringsstringLength
- length of each stringseed
- seed value for the random number generator used for the generation
of the pseudo-random string. If the same seed value is used, the
same pseudo-random string array is generated.public static java.lang.String removeLastPart(java.lang.String string, char separator)
string
- the Stringseparator
- separation characterpublic static java.lang.String replaceFromMap(java.lang.String string, java.util.Map<java.lang.String,java.lang.String> replacements)
null
, in
which case the original string is returned unchanged.
This method is semantically the same as calling
String.replace(CharSequence, CharSequence)
for each of the entries in
the map, but may be significantly faster for many replacements performed on a
short string, since String.replace(CharSequence, CharSequence)
uses
regular expressions internally and results in many String object allocations
when applied iteratively.
The order in which replacements are applied depends on the order of the map's
entry set.public static java.lang.String removeAll(java.lang.String string, java.lang.String... stringsToRemove)
public static java.lang.String normalizeLineSeparatorsPlatformSpecific(java.lang.String string)
LINE_SEPARATOR
).public static java.lang.String normalizeLineSeparatorsPlatformIndependent(java.lang.String string)
LINE_FEED
.public static java.lang.String replaceLineBreaks(java.lang.String string, java.lang.String symbol)
public static java.lang.String[] splitLines(java.lang.String content)
null
an
array of length zero is returned.splitLinesAsList(String)
public static int countCharacter(java.lang.String content, char character)
public static int countLines(java.lang.String content)
public static int countLines(java.lang.String content, boolean includeTrailingEmptyLine)
public static java.util.List<java.lang.String> splitLinesAsList(java.lang.String content)
null
an
empty list is returned.splitLines(String)
public static java.util.List<java.lang.String> splitLinesAsList(java.lang.String content, boolean includeTrailingEmptyLine)
null
an
empty list is returned.splitLines(String)
public static java.lang.String addPrefix(java.lang.String string, java.lang.String separator, java.lang.String prefix)
public static java.lang.String addSuffix(java.lang.String string, java.lang.String separator, java.lang.String suffix)
public static java.lang.String stripPrefix(java.lang.String string, java.lang.String prefix)
string
- the stringprefix
- the prefixpublic static java.lang.String stripPrefixIgnoreCase(java.lang.String string, java.lang.String prefix)
stripPrefixIgnoreCase("C:/Programs/", "c:/programs/notepad.exe")
will
return "notepad.exe"
.string
- the stringprefix
- the prefixpublic static java.lang.String stripSuffix(java.lang.String string, java.lang.String suffix)
string
- the stringsuffix
- the suffixpublic static java.lang.String stripDigits(java.lang.String string)
public static java.lang.String stripTrailingDigits(java.lang.String string)
public static java.lang.String toString(java.util.Map<?,?> map)
public static java.lang.String toString(java.util.Map<?,?> map, java.lang.String indent)
map
- the mapindent
- a line indentpublic static java.lang.String obtainStackTrace(java.lang.Throwable throwable)
Throwable
to a string.public static boolean startsWithOneOf(java.lang.String string, java.lang.String... prefixes)
false
if the list of prefixes is empty. This should only be used
for short lists of prefixes.public static boolean startsWithOneOf(java.lang.String string, java.lang.Iterable<java.lang.String> prefixes)
false
if the list of prefixes is empty. This should only be used
for short lists of prefixes. The given list must not be null.public static boolean startsWithIgnoreCase(java.lang.String string, java.lang.String prefix)
startsWithIgnoreCase("foobar", "Foo")
will return true.public static boolean containsOneOf(java.lang.String text, java.lang.String... strings)
false
if the list of strings is empty. This should only be used for short lists of
strings.public static boolean containsOneOf(java.lang.String text, java.lang.Iterable<java.lang.String> strings)
false
if the list of strings is empty. This should only be used for short lists of
strings.public static boolean endsWithIgnoreCase(java.lang.String string, java.lang.String suffix)
public static boolean endsWithOneOf(java.lang.String string, java.lang.String... suffixes)
false
if the list of prefixes is empty. This should only be used
for short lists of suffixes.public static java.lang.String prefixLines(java.lang.String string, java.lang.String prefix, boolean prefixFirstLine)
string
- the string to prefixprefix
- the prefix to addprefixFirstLine
- a flag that indicates if the first line should be prefixed or not.public static java.lang.Character[] splitChars(java.lang.String s)
Character
s. This is mostly
used for testing purposes, if an array of certain objects is needed.public static java.lang.String capitalize(java.lang.String string)
public static java.lang.String decapitalize(java.lang.String string)
public static java.lang.String wrapLongLines(java.lang.String s, int maxLineLength)
public static java.lang.String longestCommonPrefix(java.lang.String s, java.lang.String t)
public static java.lang.String longestCommonSuffix(java.lang.String s, java.lang.String t)
public static java.lang.String reverse(java.lang.String s)
public static java.lang.String longestCommonPrefix(java.lang.Iterable<java.lang.String> strings)
public static java.lang.String removeWhitespace(java.lang.String content)
public static java.lang.String removeWhitespaceAtBeginningOfLine(java.lang.String content)
public static java.lang.String createUniqueName(java.lang.String baseName, java.util.Set<java.lang.String> usedNames)
public static java.lang.String camelCaseToKebabCase(java.lang.String s)
public static java.lang.String kebabCaseToCamelCase(java.lang.String name)
public static java.lang.String camelCaseToUnderscored(java.lang.String s)
public static java.lang.String encodeAsHex(byte[] data)
public static byte[] decodeFromHex(java.lang.String s)
public static java.lang.String format(double number, java.text.NumberFormat numberFormat)
null
, this uses String.valueOf(double)
.public static java.lang.String escapeRegexReplacementString(java.lang.String replacement)
Matcher.appendReplacement(StringBuffer, String)
or
String.replaceAll(String, String)
treat dollar signs as group
references. This method escapes replacement strings so that dollar signs are
treated as literals.public static byte[] stringToBytes(java.lang.String s)
public static java.lang.String bytesToString(byte[] b)
public static java.lang.String bytesToString(byte[] b, java.nio.charset.Charset encoding)
This method returns null on a null input.
public static java.util.List<java.lang.String> asStringList(java.util.Collection<?> objects)
String.valueOf(java.lang.Object)
is used to convert each object.
null values are included, i.e., the resulting list is guaranteed to
have the size of the initial collection.public static java.util.List<java.lang.String> filterBySuffix(java.lang.String suffix, java.util.Collection<java.lang.String> strings)
public static <T> java.util.List<java.lang.String> toStrings(java.util.Collection<T> objects)
Object.toString()
on each non-null element. For null entries in the
input, the output will contain a null entry as well.public static java.lang.String[] toStringArray(java.lang.Object[] array)
public static java.io.InputStream toInputStream(java.lang.String string)
InputStream
with UTF-8 encoding.public static java.lang.String fromInputStream(java.io.InputStream inputStream) throws java.io.IOException
InputStream
to a String with UTF-8 encoding.java.io.IOException
public static java.lang.String truncate(java.lang.String string, int length, java.lang.String suffix)
public static int editDistance(java.lang.String s, java.lang.String t)
Diff
class should be used, that
has a more efficient algorithm, but only for insert/delete (not replace
operation).
Although this is a clean reimplementation, the basic algorithm is explained
here: http://en.wikipedia.org/wiki/Levenshtein_distance#
Iterative_with_two_matrix_rowspublic static boolean isEditDistanceAtMost1(java.lang.String s, java.lang.String t)
editDistance(String, String)
, is 0 or 1. This implementation is
significantly more efficient compared to actually calculating the edit
distance and runs in O(n+m).
The idea is that with at most one change, the start and end of both strings
must be the same, to traverse from start and end to the first difference. If
the distance between both pointers is at most one, the edit distance is at
most one as well.public static java.util.List<java.lang.String> lowercaseList(java.util.Collection<java.lang.String> strings)
public static java.lang.String defaultIfNull(java.lang.String input, java.lang.String defaultValue)
public static java.lang.String defaultIfNullOrEmpty(java.lang.String input, java.lang.String defaultValue)
public static java.lang.String emptyIfNull(java.lang.String input)
EMPTY_STRING
in case the input is
null.public static java.util.List<java.lang.String> splitTopLevel(java.lang.String input, char separator, char levelStart, char levelEnd)
"String,Map<String,Integer>,Map<String,Map<String,Integer>>"
split at ',' with levelStart = '<' and levelEnd = '>' would result in
["String","Map<String,Integer>","Map<String,Map<String,Integer>>"]
If there is no separator char, use the levelStart char. In this case,
separator chars are included in the output.
splitTopLevel("((a))(b)", '(', '(', ')' ) = ["", "((a))", "(b)"]
Can also be used to split columns from a CSV line where values can be quoted
splitTopLevel("\"a\";\";\";c", ';', '"', '"' ) = ["\"a\"", "\";\"", "c"]
input
- The input string.separator
- The separator character.levelStart
- The character that starts a new level.levelEnd
- The character that ends a level.java.lang.AssertionError
- If the numbers for opening and closing characters in the input
string differ.public static java.lang.String ensureEndsWith(java.lang.String s, java.lang.String suffix)
s + suffix
.public static java.lang.String ensureStartsWith(java.lang.String s, java.lang.String prefix)
public static java.lang.String concatWithEscapeCharacter(java.util.List<java.lang.String> data, java.lang.String delimiter)
public static java.util.List<java.lang.String> splitWithEscapeCharacter(java.lang.String data, java.lang.String delimiter)
public static java.lang.String applyAllReplacements(java.lang.String s, PairList<java.util.regex.Pattern,java.lang.String> replacements)
public static java.lang.String applyReplacement(java.lang.String s, java.util.regex.Pattern pattern, java.lang.String replacement)
String.replaceAll(String, String)
, but accepting a Pattern
instead of a regex string.public static java.lang.String nullIfEmpty(java.lang.String input)
null
if the input string is empty (after trimming) or
null
. Otherwise, the input is returned unaltered.public static boolean isInteger(java.lang.String string)
public static int indexOfMatch(java.lang.String string, java.util.regex.Pattern pattern)
public static java.lang.String escapeChars(java.lang.String content, java.util.List<java.lang.Character> chars)
public static java.lang.String unEscapeChars(java.lang.String content, java.util.List<java.lang.Character> chars)
public static java.lang.String getBeginning(java.lang.String s, int numberOfChars)
public static java.lang.String toFirstUpper(java.lang.String s)
public static java.lang.String pluralize(java.lang.String string, int count)
public static java.lang.String surroundWith(java.lang.String s, java.lang.String prefix, java.lang.String suffix)
public static boolean equalsOneOf(java.lang.String value, java.lang.String... strings)
Collection.contains(..)
. Consider putting the arguments into a
Collection constant.public static java.lang.String removeDoubleQuotes(java.lang.String string)
public static java.lang.String removeSingleQuotes(java.lang.String string)
public static java.lang.String repeat(java.lang.String s, int times)
String
s
- the String
to repeattimes
- number of times the string gets repeatedString
public static java.lang.String safeToString(java.lang.Object value)
Object.toString()
with null check.value
- object to stringifyEMPTY_STRING
if value is
null.public static java.lang.String retainHeadLines(java.lang.String text, int numberOfLines)