public class Strings extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
EMPTY
The empty String
"". |
static int |
INDEX_NOT_FOUND
Represents a failed index search.
|
static String |
SPACE
A String for a space character.
|
static String |
WHITESPACE |
| 限定符和类型 | 方法和说明 |
|---|---|
static String |
capitalize(String str)
Capitalizes a String changing the first letter to title case as
per
Character.toTitleCase(char). |
static int |
compare(String str1,
String str2)
Compare two Strings lexicographically, as per
String.compareTo(String), returning :
int = 0, if str1 is equal to str2 (or both null)
int < 0, if str1 is less than str2
int > 0, if str1 is greater than str2
This is a null safe version of :
str1.compareTo(str2)
null value is considered less than non-null value. |
static int |
compare(String str1,
String str2,
boolean nullIsLess)
Compare two Strings lexicographically, as per
String.compareTo(String), returning :
int = 0, if str1 is equal to str2 (or both null)
int < 0, if str1 is less than str2
int > 0, if str1 is greater than str2
This is a null safe version of :
str1.compareTo(str2)
null inputs are handled according to the nullIsLess parameter. |
static int |
compareIgnoreCase(String str1,
String str2)
Compare two Strings lexicographically, ignoring case differences,
as per
String.compareToIgnoreCase(String), returning :
int = 0, if str1 is equal to str2 (or both null)
int < 0, if str1 is less than str2
int > 0, if str1 is greater than str2
This is a null safe version of :
str1.compareToIgnoreCase(str2)
null value is considered less than non-null value. |
static int |
compareIgnoreCase(String str1,
String str2,
boolean nullIsLess)
Compare two Strings lexicographically, ignoring case differences,
as per
String.compareToIgnoreCase(String), returning :
int = 0, if str1 is equal to str2 (or both null)
int < 0, if str1 is less than str2
int > 0, if str1 is greater than str2
This is a null safe version of :
str1.compareToIgnoreCase(str2)
null inputs are handled according to the nullIsLess parameter. |
static String |
completingLength(String str,
int expectedLength,
char c,
boolean left) |
static boolean |
containsAny(CharSequence cs,
char... searchChars)
Checks if the CharSequence contains any character in the given
set of characters.
|
static boolean |
containsAny(CharSequence cs,
CharSequence searchChars)
Checks if the CharSequence contains any character in the given set of characters.
|
static <T> T |
convertTo(String str,
Class<T> targetClass) |
static int |
decodeHexNibble(char c)
Helper to decode half of a hexadecimal number from a string.
|
static String |
deleteWhitespace(String str)
Deletes all whitespaces from a String as defined by
Character.isWhitespace(char). |
static boolean |
endsWith(CharSequence str,
CharSequence suffix)
Check if a CharSequence ends with a specified suffix.
|
static boolean |
endsWithIgnoreCase(CharSequence str,
CharSequence suffix)
Case insensitive check if a CharSequence ends with a specified suffix.
|
static boolean |
equals(CharSequence cs1,
CharSequence cs2)
Compares two CharSequences, returning
true if they represent
equal sequences of characters. |
static boolean |
equalsAny(CharSequence string,
CharSequence... searchStrings)
Compares given
string to a CharSequences vararg of searchStrings,
returning true if the string is equal to any of the searchStrings. |
static boolean |
equalsAnyIgnoreCase(CharSequence string,
CharSequence... searchStrings)
Compares given
string to a CharSequences vararg of searchStrings,
returning true if the string is equal to any of the searchStrings, ignoring case. |
static boolean |
equalsIgnoreCase(CharSequence cs1,
CharSequence cs2)
Compares two CharSequences, returning
true if they represent
equal sequences of characters, ignoring case. |
static byte[] |
getBytesUtf8(String string) |
static String |
getEmptyIfBlank(String str) |
static String |
getEmptyIfNull(String str) |
static String |
getNullIfBlank(String str) |
static String |
getNullIfEmpty(String str) |
static int |
indexOf(CharSequence seq,
CharSequence searchSeq)
Finds the first index within a CharSequence, handling
null. |
static int |
indexOf(CharSequence seq,
CharSequence searchSeq,
int startPos)
Finds the first index within a CharSequence, handling
null. |
static int |
indexOf(CharSequence seq,
int searchChar)
Returns the index within
seq of the first occurrence of
the specified character. |
static int |
indexOf(CharSequence seq,
int searchChar,
int startPos)
Returns the index within
seq of the first occurrence of the
specified character, starting the search at the specified index. |
static String |
insert(String string,
List<Integer> slotIndexes,
String insertment) |
static boolean |
isBlank(String str)
judge a string has some whitespace at most
|
static boolean |
isEmpty(CharSequence str) |
static boolean |
isEmpty(String str)
judge a string is null or ""
|
static boolean |
isNotBlank(String str) |
static boolean |
isNotEmpty(String str) |
static boolean |
isNumeric(CharSequence cs) |
static boolean |
isVowelLetter(char c) |
static String |
join(String separator,
Byte[] objects) |
static String |
join(String separator,
Character[] objects) |
static String |
join(String separator,
Double[] objects) |
static String |
join(String separator,
Float[] objects) |
static String |
join(String separator,
Integer[] objects) |
static String |
join(String separator,
Iterable objects) |
static String |
join(String separator,
Iterator objects)
append all objects with the specified separator
|
static String |
join(String separator,
Long[] objects) |
static String |
join(String separator,
Object[] objects) |
static String |
join(String separator,
Short[] objects) |
static String |
join(String separator,
String[] objects) |
static String |
leftPad(String str,
int size)
Left pad a String with spaces (' ').
|
static String |
leftPad(String str,
int size,
char padChar)
Left pad a String with a specified charactfer.
|
static String |
leftPad(String str,
int size,
String padStr)
Left pad a String with a specified String.
|
static String |
lowerCase(String str)
Converts a String to lower case as per
String.toLowerCase(). |
static String |
lowerCase(String str,
int offset,
int limit) |
static String |
lowerCase(String str,
Locale locale)
Converts a String to lower case as per
String.toLowerCase(Locale). |
static String |
newStringUsAscii(byte[] bytes)
Constructs a new
String by decoding the specified array of bytes using the US-ASCII charset. |
static String |
newStringUtf8(byte[] bytes)
Constructs a new
String by decoding the specified array of bytes using the UTF-8 charset. |
static String |
remove(String str,
char remove)
Removes all occurrences of a character from within the source string.
|
static String |
remove(String str,
String remove)
Removes all occurrences of a substring from within the source string.
|
static String |
removeDuplicateWhitespace(String s)
Remove all duplicate whitespace characters and line terminators are replaced with a single space.
|
static String |
removeEnd(String str,
String remove)
Removes a substring only if it is at the end of a source string,
otherwise returns the source string.
|
static String |
removeEndIgnoreCase(String str,
String remove)
Case insensitive removal of a substring if it is at the end of a source string,
otherwise returns the source string.
|
static String |
removePattern(String source,
String regex)
Removes each substring of the source String that matches the given regular expression using the DOTALL option.
|
static String |
removeStart(String str,
String remove)
Removes a substring only if it is at the beginning of a source string,
otherwise returns the source string.
|
static String |
removeStartIgnoreCase(String str,
String remove)
Case insensitive removal of a substring if it is at the beginning of a source string,
otherwise returns the source string.
|
static String |
repeat(char ch,
int repeat)
Returns padding using the specified delimiter repeated
to a given length.
|
static String |
repeat(String str,
int repeat)
Repeat a String
repeat times to form a
new String. |
static String |
repeat(String str,
String separator,
int repeat)
Repeat a String
repeat times to form a
new String, with a String separator injected each time. |
static String |
replace(String text,
String searchString,
String replacement)
Replaces all occurrences of a String within another String.
|
static String |
replace(String text,
String searchString,
String replacement,
int max)
Replaces a String with another String inside a larger String,
for the first
max values of the search String. |
static String |
replaceChars(String str,
char searchChar,
char replaceChar)
Replaces all occurrences of a character in a String with another.
|
static String |
replaceChars(String str,
String searchChars,
String replaceChars)
Replaces multiple characters in a String in one go.
|
static String |
replaceEach(String text,
String[] searchList,
String[] replacementList)
Replaces all occurrences of Strings within another String.
|
static String |
replaceEachRepeatedly(String text,
String[] searchList,
String[] replacementList)
Replaces all occurrences of Strings within another String.
|
static String |
replaceOnce(String text,
String searchString,
String replacement)
Replaces a String with another String inside a larger String, once.
|
static String |
replacePattern(String source,
String regex,
String replacement)
Replaces each substring of the source String that matches the given regular expression with the given
replacement using the
Pattern.DOTALL option. |
static String |
rightPad(String str,
int size)
Right pad a String with spaces (' ').
|
static String |
rightPad(String str,
int size,
char padChar)
Right pad a String with a specified character.
|
static String |
rightPad(String str,
int size,
String padStr)
Right pad a String with a specified String.
|
static String[] |
split(String string,
String separator)
split a string, the returned array is not contains: whitespace, null.
|
static boolean |
startsWith(CharSequence str,
CharSequence prefix) |
static boolean |
startsWith(CharSequence str,
CharSequence prefix,
boolean ignoreCase)
Check if a CharSequence starts with a specified prefix (optionally case insensitive).
|
static boolean |
startsWithIgnoreCase(CharSequence str,
CharSequence prefix)
Case insensitive check if a CharSequence starts with a specified prefix.
|
static boolean |
startsWithVowelLetter(String string) |
static String |
strip(String str)
Strips whitespace from the start and end of a String.
|
static String |
strip(String str,
String stripChars)
Strips any of a set of characters from the start and end of a String.
|
static String |
stripAccents(String input)
Removes diacritics (~= accents) from a string.
|
static String[] |
stripAll(String... strs)
Strips whitespace from the start and end of every String in an array.
|
static String[] |
stripAll(String[] strs,
String stripChars)
Strips any of a set of characters from the start and end of every
String in an array.
|
static String |
stripEnd(String str,
String stripChars)
Strips any of a set of characters from the end of a String.
|
static String |
stripStart(String str,
String stripChars)
Strips any of a set of characters from the start of a String.
|
static String |
stripToEmpty(String str)
Strips whitespace from the start and end of a String returning
an empty String if
null input. |
static String |
stripToNull(String str)
Strips whitespace from the start and end of a String returning
null if the String is empty ("") after the strip. |
static CharSequence |
subSequence(CharSequence cs,
int start)
Returns a new
CharSequence that is a subsequence of this
sequence starting with the char value at the specified index. |
static boolean |
substringMatch(CharSequence str,
int index,
CharSequence substring)
Test whether the given string matches the given substring
at the given index.
|
static String |
swapCase(String str)
Swaps the case of a String changing upper and title case to
lower case, and lower case to upper case.
|
static String |
trim(String str)
Removes control characters (char <= 32) from both
ends of this String, handling
null by returning
null. |
static String |
trimOrEmpty(String str)
Trim a string, if the string is null, will return empty string:""
|
static String |
trimToEmpty(String str)
Removes control characters (char <= 32) from both
ends of this String returning an empty String ("") if the String
is empty ("") after the trim or if it is
null. |
static String |
trimToNull(String str)
Removes control characters (char <= 32) from both
ends of this String returning
null if the String is
empty ("") after the trim or if it is null. |
static String |
truncate(String str,
int maxWidth)
Truncates a String.
|
static String |
truncate(String str,
int offset,
int maxWidth)
Truncates a String.
|
static String |
uncapitalize(String str)
Uncapitalizes a String changing the first letter to title case as
per
Character.toLowerCase(char). |
static String |
unifyLineSeparators(String s) |
static String |
unifyLineSeparators(String s,
String lineSeparator)
Parses the given String and replaces all occurrences of '\n', '\r' and '\r\n' with the system line separator.
|
static String |
upperCase(String str)
Converts a String to upper case as per
String.toUpperCase(). |
static String |
upperCase(String str,
int offset,
int limit) |
static String |
upperCase(String str,
Locale locale)
Converts a String to upper case as per
String.toUpperCase(Locale). |
static String |
useValueIfBlank(String str,
String defaultValue) |
static String |
useValueIfEmpty(String str,
String defaultValue) |
static String |
useValueIfNull(String str,
String defaultValue)
equals:
return str == null ? |
public static final int INDEX_NOT_FOUND
public static boolean isEmpty(String str)
str - the specified stringpublic static boolean isEmpty(CharSequence str)
public static boolean isNotEmpty(String str)
public static boolean isBlank(String str)
str - the specified stringpublic static boolean isNotBlank(String str)
public static String useValueIfNull(String str, String defaultValue)
return str == null ? defaultValue : str;
str - a specified stringdefaultValue - the default valuepublic static String trimOrEmpty(String str)
str - a specified stringpublic static String join(@NonNull String separator, @Nullable Iterator objects)
separator - the specified separatorobjects - the dbjects that will be appendpublic static String insert(@NonNull String string, @Nullable List<Integer> slotIndexes, @NonNull String insertment)
public static String[] split(@Nullable String string, @Nullable String separator)
public static int decodeHexNibble(char c)
c - The ASCII character of the hexadecimal number to decode.
Must be in the range [0-9a-fA-F].-1 if the character is invalid.public static boolean substringMatch(CharSequence str, int index, CharSequence substring)
str - the original string (or StringBuilder)index - the index in the original string to start matching againstsubstring - the substring to match at the given indexpublic static byte[] getBytesUtf8(String string)
public static String newStringUtf8(byte[] bytes)
String by decoding the specified array of bytes using the UTF-8 charset.bytes - The bytes to be decoded into charactersString decoded from the specified array of bytes using the UTF-8 charset,
or null if the input byte array was null.NullPointerException - Thrown if Charsets.UTF_8 is not initialized, which should never happen since it is
required by the Java platform specification.public static String newStringUsAscii(byte[] bytes)
String by decoding the specified array of bytes using the US-ASCII charset.bytes - The bytes to be decoded into charactersString decoded from the specified array of bytes using the US-ASCII charset,
or null if the input byte array was null.NullPointerException - Thrown if Charsets.US_ASCII is not initialized, which should never happen since it is
required by the Java platform specification.public static String deleteWhitespace(String str)
Deletes all whitespaces from a String as defined by
Character.isWhitespace(char).
StringUtils.deleteWhitespace(null) = null
StringUtils.deleteWhitespace("") = ""
StringUtils.deleteWhitespace("abc") = "abc"
StringUtils.deleteWhitespace(" ab c ") = "abc"
str - the String to delete whitespace from, may be nullnull if null String inputpublic static String removeDuplicateWhitespace(String s)
s - a not null Stringpublic static String unifyLineSeparators(String s, String lineSeparator)
s - a not null StringlineSeparator - the wanted line separator ("\n" on UNIX), if null using the System line separator.IllegalArgumentException - if ls is not '\n', '\r' and '\r\n' characters.public static boolean isNumeric(CharSequence cs)
public static boolean startsWithIgnoreCase(CharSequence str, CharSequence prefix)
Case insensitive check if a CharSequence starts with a specified prefix.
nulls are handled without exceptions. Two null
references are considered to be equal. The comparison is case insensitive.
StringUtils.startsWithIgnoreCase(null, null) = true
StringUtils.startsWithIgnoreCase(null, "abc") = false
StringUtils.startsWithIgnoreCase("abcdef", null) = false
StringUtils.startsWithIgnoreCase("abcdef", "abc") = true
StringUtils.startsWithIgnoreCase("ABCDEF", "abc") = true
str - the CharSequence to check, may be nullprefix - the prefix to find, may be nulltrue if the CharSequence starts with the prefix, case insensitive, or
both nullChanged signature from startsWithIgnoreCase(String, String) to startsWithIgnoreCase(CharSequence, CharSequence)public static boolean startsWith(CharSequence str, CharSequence prefix)
public static boolean startsWith(CharSequence str, CharSequence prefix, boolean ignoreCase)
Check if a CharSequence starts with a specified prefix (optionally case insensitive).
str - the CharSequence to check, may be nullprefix - the prefix to find, may be nullignoreCase - indicates whether the compare should ignore case
(case insensitive) or not.true if the CharSequence starts with the prefix or
both nullString.startsWith(String)public static boolean endsWith(CharSequence str, CharSequence suffix)
Check if a CharSequence ends with a specified suffix.
nulls are handled without exceptions. Two null
references are considered to be equal. The comparison is case sensitive.
StringUtils.endsWith(null, null) = true
StringUtils.endsWith(null, "def") = false
StringUtils.endsWith("abcdef", null) = false
StringUtils.endsWith("abcdef", "def") = true
StringUtils.endsWith("ABCDEF", "def") = false
StringUtils.endsWith("ABCDEF", "cde") = false
str - the CharSequence to check, may be nullsuffix - the suffix to find, may be nulltrue if the CharSequence ends with the suffix, case sensitive, or
both null
Changed signature from endsWith(String, String) to endsWith(CharSequence, CharSequence)
public static boolean endsWithIgnoreCase(CharSequence str, CharSequence suffix)
Case insensitive check if a CharSequence ends with a specified suffix.
nulls are handled without exceptions. Two null
references are considered to be equal. The comparison is case insensitive.
StringUtils.endsWithIgnoreCase(null, null) = true
StringUtils.endsWithIgnoreCase(null, "def") = false
StringUtils.endsWithIgnoreCase("abcdef", null) = false
StringUtils.endsWithIgnoreCase("abcdef", "def") = true
StringUtils.endsWithIgnoreCase("ABCDEF", "def") = true
StringUtils.endsWithIgnoreCase("ABCDEF", "cde") = false
str - the CharSequence to check, may be nullsuffix - the suffix to find, may be nulltrue if the CharSequence ends with the suffix, case insensitive, or
both null
Changed signature from endsWithIgnoreCase(String, String) to endsWithIgnoreCase(CharSequence, CharSequence)
public static boolean containsAny(CharSequence cs, char... searchChars)
Checks if the CharSequence contains any character in the given set of characters.
A null CharSequence will return false.
A null or zero length search array will return false.
StringUtils.containsAny(null, *) = false
StringUtils.containsAny("", *) = false
StringUtils.containsAny(*, null) = false
StringUtils.containsAny(*, []) = false
StringUtils.containsAny("zzabyycdxx",['z','a']) = true
StringUtils.containsAny("zzabyycdxx",['b','y']) = true
StringUtils.containsAny("aba", ['z']) = false
cs - the CharSequence to check, may be nullsearchChars - the chars to search for, may be nulltrue if any of the chars are found,
false if no match or null input
Changed signature from containsAny(String, char[]) to containsAny(CharSequence, char...)
public static boolean containsAny(CharSequence cs, CharSequence searchChars)
Checks if the CharSequence contains any character in the given set of characters.
A null CharSequence will return false. A null search CharSequence will return
false.
StringUtils.containsAny(null, *) = false
StringUtils.containsAny("", *) = false
StringUtils.containsAny(*, null) = false
StringUtils.containsAny(*, "") = false
StringUtils.containsAny("zzabyycdxx", "za") = true
StringUtils.containsAny("zzabyycdxx", "by") = true
StringUtils.containsAny("aba","z") = false
cs - the CharSequence to check, may be nullsearchChars - the chars to search for, may be nulltrue if any of the chars are found, false if no match or null input
Changed signature from containsAny(String, String) to containsAny(CharSequence, CharSequence)
public static String repeat(String str, int repeat)
Repeat a String repeat times to form a
new String.
StringUtils.repeat(null, 2) = null
StringUtils.repeat("", 0) = ""
StringUtils.repeat("", 2) = ""
StringUtils.repeat("a", 3) = "aaa"
StringUtils.repeat("ab", 2) = "abab"
StringUtils.repeat("a", -2) = ""
str - the String to repeat, may be nullrepeat - number of times to repeat str, negative treated as zeronull if null String inputpublic static String repeat(String str, String separator, int repeat)
Repeat a String repeat times to form a
new String, with a String separator injected each time.
StringUtils.repeat(null, null, 2) = null
StringUtils.repeat(null, "x", 2) = null
StringUtils.repeat("", null, 0) = ""
StringUtils.repeat("", "", 2) = ""
StringUtils.repeat("", "x", 3) = "xxx"
StringUtils.repeat("?", ", ", 3) = "?, ?, ?"
str - the String to repeat, may be nullseparator - the String to inject, may be nullrepeat - number of times to repeat str, negative treated as zeronull if null String inputpublic static String repeat(char ch, int repeat)
Returns padding using the specified delimiter repeated to a given length.
StringUtils.repeat('e', 0) = ""
StringUtils.repeat('e', 3) = "eee"
StringUtils.repeat('e', -2) = ""
Note: this method doesn't not support padding with
Unicode Supplementary Characters
as they require a pair of chars to be represented.
If you are needing to support full I18N of your applications
consider using repeat(String, int) instead.
ch - character to repeatrepeat - number of times to repeat char, negative treated as zerorepeat(String, int)public static String rightPad(String str, int size)
Right pad a String with spaces (' ').
The String is padded to the size of size.
StringUtils.rightPad(null, *) = null
StringUtils.rightPad("", 3) = " "
StringUtils.rightPad("bat", 3) = "bat"
StringUtils.rightPad("bat", 5) = "bat "
StringUtils.rightPad("bat", 1) = "bat"
StringUtils.rightPad("bat", -1) = "bat"
str - the String to pad out, may be nullsize - the size to pad tonull if null String inputpublic static String rightPad(String str, int size, char padChar)
Right pad a String with a specified character.
The String is padded to the size of size.
StringUtils.rightPad(null, *, *) = null
StringUtils.rightPad("", 3, 'z') = "zzz"
StringUtils.rightPad("bat", 3, 'z') = "bat"
StringUtils.rightPad("bat", 5, 'z') = "batzz"
StringUtils.rightPad("bat", 1, 'z') = "bat"
StringUtils.rightPad("bat", -1, 'z') = "bat"
str - the String to pad out, may be nullsize - the size to pad topadChar - the character to pad withnull if null String inputpublic static String rightPad(String str, int size, String padStr)
Right pad a String with a specified String.
The String is padded to the size of size.
StringUtils.rightPad(null, *, *) = null
StringUtils.rightPad("", 3, "z") = "zzz"
StringUtils.rightPad("bat", 3, "yz") = "bat"
StringUtils.rightPad("bat", 5, "yz") = "batyz"
StringUtils.rightPad("bat", 8, "yz") = "batyzyzy"
StringUtils.rightPad("bat", 1, "yz") = "bat"
StringUtils.rightPad("bat", -1, "yz") = "bat"
StringUtils.rightPad("bat", 5, null) = "bat "
StringUtils.rightPad("bat", 5, "") = "bat "
str - the String to pad out, may be nullsize - the size to pad topadStr - the String to pad with, null or empty treated as single spacenull if null String inputpublic static String leftPad(String str, int size)
Left pad a String with spaces (' ').
The String is padded to the size of size.
StringUtils.leftPad(null, *) = null
StringUtils.leftPad("", 3) = " "
StringUtils.leftPad("bat", 3) = "bat"
StringUtils.leftPad("bat", 5) = " bat"
StringUtils.leftPad("bat", 1) = "bat"
StringUtils.leftPad("bat", -1) = "bat"
str - the String to pad out, may be nullsize - the size to pad tonull if null String inputpublic static String leftPad(String str, int size, char padChar)
Left pad a String with a specified charactfer.
Pad to a size of size.
StringUtils.leftPad(null, *, *) = null
StringUtils.leftPad("", 3, 'z') = "zzz"
StringUtils.leftPad("bat", 3, 'z') = "bat"
StringUtils.leftPad("bat", 5, 'z') = "zzbat"
StringUtils.leftPad("bat", 1, 'z') = "bat"
StringUtils.leftPad("bat", -1, 'z') = "bat"
str - the String to pad out, may be nullsize - the size to pad topadChar - the character to pad withnull if null String inputpublic static String leftPad(String str, int size, String padStr)
Left pad a String with a specified String.
Pad to a size of size.
StringUtils.leftPad(null, *, *) = null
StringUtils.leftPad("", 3, "z") = "zzz"
StringUtils.leftPad("bat", 3, "yz") = "bat"
StringUtils.leftPad("bat", 5, "yz") = "yzbat"
StringUtils.leftPad("bat", 8, "yz") = "yzyzybat"
StringUtils.leftPad("bat", 1, "yz") = "bat"
StringUtils.leftPad("bat", -1, "yz") = "bat"
StringUtils.leftPad("bat", 5, null) = " bat"
StringUtils.leftPad("bat", 5, "") = " bat"
str - the String to pad out, may be nullsize - the size to pad topadStr - the String to pad with, null or empty treated as single spacenull if null String inputpublic static String removeStart(String str, String remove)
Removes a substring only if it is at the beginning of a source string, otherwise returns the source string.
A null source string will return null.
An empty ("") source string will return the empty string.
A null search string will return the source string.
StringUtils.removeStart(null, *) = null
StringUtils.removeStart("", *) = ""
StringUtils.removeStart(*, null) = *
StringUtils.removeStart("www.domain.com", "www.") = "domain.com"
StringUtils.removeStart("domain.com", "www.") = "domain.com"
StringUtils.removeStart("www.domain.com", "domain") = "www.domain.com"
StringUtils.removeStart("abc", "") = "abc"
str - the source String to search, may be nullremove - the String to search for and remove, may be nullnull if null String inputpublic static String removeStartIgnoreCase(String str, String remove)
Case insensitive removal of a substring if it is at the beginning of a source string, otherwise returns the source string.
A null source string will return null.
An empty ("") source string will return the empty string.
A null search string will return the source string.
StringUtils.removeStartIgnoreCase(null, *) = null
StringUtils.removeStartIgnoreCase("", *) = ""
StringUtils.removeStartIgnoreCase(*, null) = *
StringUtils.removeStartIgnoreCase("www.domain.com", "www.") = "domain.com"
StringUtils.removeStartIgnoreCase("www.domain.com", "WWW.") = "domain.com"
StringUtils.removeStartIgnoreCase("domain.com", "www.") = "domain.com"
StringUtils.removeStartIgnoreCase("www.domain.com", "domain") = "www.domain.com"
StringUtils.removeStartIgnoreCase("abc", "") = "abc"
str - the source String to search, may be nullremove - the String to search for (case insensitive) and remove, may be nullnull if null String inputpublic static String removeEnd(String str, String remove)
Removes a substring only if it is at the end of a source string, otherwise returns the source string.
A null source string will return null.
An empty ("") source string will return the empty string.
A null search string will return the source string.
StringUtils.removeEnd(null, *) = null
StringUtils.removeEnd("", *) = ""
StringUtils.removeEnd(*, null) = *
StringUtils.removeEnd("www.domain.com", ".com.") = "www.domain.com"
StringUtils.removeEnd("www.domain.com", ".com") = "www.domain"
StringUtils.removeEnd("www.domain.com", "domain") = "www.domain.com"
StringUtils.removeEnd("abc", "") = "abc"
str - the source String to search, may be nullremove - the String to search for and remove, may be nullnull if null String inputpublic static String removeEndIgnoreCase(String str, String remove)
Case insensitive removal of a substring if it is at the end of a source string, otherwise returns the source string.
A null source string will return null.
An empty ("") source string will return the empty string.
A null search string will return the source string.
StringUtils.removeEndIgnoreCase(null, *) = null
StringUtils.removeEndIgnoreCase("", *) = ""
StringUtils.removeEndIgnoreCase(*, null) = *
StringUtils.removeEndIgnoreCase("www.domain.com", ".com.") = "www.domain.com"
StringUtils.removeEndIgnoreCase("www.domain.com", ".com") = "www.domain"
StringUtils.removeEndIgnoreCase("www.domain.com", "domain") = "www.domain.com"
StringUtils.removeEndIgnoreCase("abc", "") = "abc"
StringUtils.removeEndIgnoreCase("www.domain.com", ".COM") = "www.domain")
StringUtils.removeEndIgnoreCase("www.domain.COM", ".com") = "www.domain")
str - the source String to search, may be nullremove - the String to search for (case insensitive) and remove, may be nullnull if null String inputpublic static String remove(String str, String remove)
Removes all occurrences of a substring from within the source string.
A null source string will return null.
An empty ("") source string will return the empty string.
A null remove string will return the source string.
An empty ("") remove string will return the source string.
StringUtils.remove(null, *) = null
StringUtils.remove("", *) = ""
StringUtils.remove(*, null) = *
StringUtils.remove(*, "") = *
StringUtils.remove("queued", "ue") = "qd"
StringUtils.remove("queued", "zz") = "queued"
str - the source String to search, may be nullremove - the String to search for and remove, may be nullnull if null String inputpublic static String remove(String str, char remove)
Removes all occurrences of a character from within the source string.
A null source string will return null.
An empty ("") source string will return the empty string.
StringUtils.remove(null, *) = null
StringUtils.remove("", *) = ""
StringUtils.remove("queued", 'u') = "qeed"
StringUtils.remove("queued", 'z') = "queued"
str - the source String to search, may be nullremove - the char to search for and remove, may be nullnull if null String inputpublic static String replaceOnce(String text, String searchString, String replacement)
Replaces a String with another String inside a larger String, once.
A null reference passed to this method is a no-op.
StringUtils.replaceOnce(null, *, *) = null
StringUtils.replaceOnce("", *, *) = ""
StringUtils.replaceOnce("any", null, *) = "any"
StringUtils.replaceOnce("any", *, null) = "any"
StringUtils.replaceOnce("any", "", *) = "any"
StringUtils.replaceOnce("aba", "a", null) = "aba"
StringUtils.replaceOnce("aba", "a", "") = "ba"
StringUtils.replaceOnce("aba", "a", "z") = "zba"
text - text to search and replace in, may be nullsearchString - the String to search for, may be nullreplacement - the String to replace with, may be nullnull if null String inputreplace(String text, String searchString, String replacement, int max)public static String replacePattern(String source, String regex, String replacement)
Pattern.DOTALL option. DOTALL is also know as single-line mode in Perl. This call
is also equivalent to:
source.replaceAll("(?s)" + regex, replacement)Pattern.compile(regex, Pattern.DOTALL).matcher(source).replaceAll(replacement)source - the source stringregex - the regular expression to which this string is to be matchedreplacement - the string to be substituted for each matchStringString.replaceAll(String, String),
Pattern.DOTALLpublic static String removePattern(String source, String regex)
source - the source stringregex - the regular expression to which this string is to be matchedStringString.replaceAll(String, String),
Pattern.DOTALLpublic static String replace(String text, String searchString, String replacement)
Replaces all occurrences of a String within another String.
A null reference passed to this method is a no-op.
StringUtils.replace(null, *, *) = null
StringUtils.replace("", *, *) = ""
StringUtils.replace("any", null, *) = "any"
StringUtils.replace("any", *, null) = "any"
StringUtils.replace("any", "", *) = "any"
StringUtils.replace("aba", "a", null) = "aba"
StringUtils.replace("aba", "a", "") = "b"
StringUtils.replace("aba", "a", "z") = "zbz"
text - text to search and replace in, may be nullsearchString - the String to search for, may be nullreplacement - the String to replace it with, may be nullnull if null String inputreplace(String text, String searchString, String replacement, int max)public static String replace(String text, String searchString, String replacement, int max)
Replaces a String with another String inside a larger String,
for the first max values of the search String.
A null reference passed to this method is a no-op.
StringUtils.replace(null, *, *, *) = null
StringUtils.replace("", *, *, *) = ""
StringUtils.replace("any", null, *, *) = "any"
StringUtils.replace("any", *, null, *) = "any"
StringUtils.replace("any", "", *, *) = "any"
StringUtils.replace("any", *, *, 0) = "any"
StringUtils.replace("abaa", "a", null, -1) = "abaa"
StringUtils.replace("abaa", "a", "", -1) = "b"
StringUtils.replace("abaa", "a", "z", 0) = "abaa"
StringUtils.replace("abaa", "a", "z", 1) = "zbaa"
StringUtils.replace("abaa", "a", "z", 2) = "zbza"
StringUtils.replace("abaa", "a", "z", -1) = "zbzz"
text - text to search and replace in, may be nullsearchString - the String to search for, may be nullreplacement - the String to replace it with, may be nullmax - maximum number of values to replace, or -1 if no maximumnull if null String inputpublic static String replaceEach(String text, String[] searchList, String[] replacementList)
Replaces all occurrences of Strings within another String.
A null reference passed to this method is a no-op, or if
any "search string" or "string to replace" is null, that replace will be
ignored. This will not repeat. For repeating replaces, call the
overloaded method.
StringUtils.replaceEach(null, *, *) = null
StringUtils.replaceEach("", *, *) = ""
StringUtils.replaceEach("aba", null, null) = "aba"
StringUtils.replaceEach("aba", new String[0], null) = "aba"
StringUtils.replaceEach("aba", null, new String[0]) = "aba"
StringUtils.replaceEach("aba", new String[]{"a"}, null) = "aba"
StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""}) = "b"
StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"}) = "aba"
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}) = "wcte"
(example of how it does not repeat)
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}) = "dcte"
text - text to search and replace in, no-op if nullsearchList - the Strings to search for, no-op if nullreplacementList - the Strings to replace them with, no-op if nullnull if
null String inputIllegalArgumentException - if the lengths of the arrays are not the same (null is ok,
and/or size 0)public static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)
Replaces all occurrences of Strings within another String.
A null reference passed to this method is a no-op, or if
any "search string" or "string to replace" is null, that replace will be
ignored.
StringUtils.replaceEach(null, *, *, *) = null
StringUtils.replaceEach("", *, *, *) = ""
StringUtils.replaceEach("aba", null, null, *) = "aba"
StringUtils.replaceEach("aba", new String[0], null, *) = "aba"
StringUtils.replaceEach("aba", null, new String[0], *) = "aba"
StringUtils.replaceEach("aba", new String[]{"a"}, null, *) = "aba"
StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""}, *) = "b"
StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"}, *) = "aba"
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}, *) = "wcte"
(example of how it repeats)
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}, false) = "dcte"
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}, true) = "tcte"
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}, true) = IllegalStateException
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}, false) = "dcabe"
text - text to search and replace in, no-op if nullsearchList - the Strings to search for, no-op if nullreplacementList - the Strings to replace them with, no-op if nullnull if
null String inputIllegalStateException - if the search is repeating and there is an endless loop due
to outputs of one being inputs to anotherIllegalArgumentException - if the lengths of the arrays are not the same (null is ok,
and/or size 0)public static String replaceChars(String str, char searchChar, char replaceChar)
Replaces all occurrences of a character in a String with another.
This is a null-safe version of String.replace(char, char).
A null string input returns null.
An empty ("") string input returns an empty string.
StringUtils.replaceChars(null, *, *) = null
StringUtils.replaceChars("", *, *) = ""
StringUtils.replaceChars("abcba", 'b', 'y') = "aycya"
StringUtils.replaceChars("abcba", 'z', 'y') = "abcba"
str - String to replace characters in, may be nullsearchChar - the character to search for, may be nullreplaceChar - the character to replace, may be nullnull if null string inputpublic static String replaceChars(String str, String searchChars, String replaceChars)
Replaces multiple characters in a String in one go. This method can also be used to delete characters.
For example:
replaceChars("hello", "ho", "jy") = jelly.
A null string input returns null.
An empty ("") string input returns an empty string.
A null or empty set of search characters returns the input string.
The length of the search characters should normally equal the length of the replace characters. If the search characters is longer, then the extra search characters are deleted. If the search characters is shorter, then the extra replace characters are ignored.
StringUtils.replaceChars(null, *, *) = null
StringUtils.replaceChars("", *, *) = ""
StringUtils.replaceChars("abc", null, *) = "abc"
StringUtils.replaceChars("abc", "", *) = "abc"
StringUtils.replaceChars("abc", "b", null) = "ac"
StringUtils.replaceChars("abc", "b", "") = "ac"
StringUtils.replaceChars("abcba", "bc", "yz") = "ayzya"
StringUtils.replaceChars("abcba", "bc", "y") = "ayya"
StringUtils.replaceChars("abcba", "bc", "yzx") = "ayzya"
str - String to replace characters in, may be nullsearchChars - a set of characters to search for, may be nullreplaceChars - a set of characters to replace, may be nullnull if null string inputpublic static String upperCase(String str)
Converts a String to upper case as per String.toUpperCase().
A null input String returns null.
StringUtils.upperCase(null) = null
StringUtils.upperCase("") = ""
StringUtils.upperCase("aBc") = "ABC"
Note: As described in the documentation for String.toUpperCase(),
the result of this method is affected by the current locale.
For platform-independent case transformations, the method lowerCase(String, Locale)
should be used with a specific locale (e.g. Locale.ENGLISH).
str - the String to upper case, may be nullnull if null String inputpublic static String upperCase(String str, Locale locale)
Converts a String to upper case as per String.toUpperCase(Locale).
A null input String returns null.
StringUtils.upperCase(null, Locale.ENGLISH) = null
StringUtils.upperCase("", Locale.ENGLISH) = ""
StringUtils.upperCase("aBc", Locale.ENGLISH) = "ABC"
str - the String to upper case, may be nulllocale - the locale that defines the case transformation rules, must not be nullnull if null String inputpublic static String lowerCase(String str)
Converts a String to lower case as per String.toLowerCase().
A null input String returns null.
StringUtils.lowerCase(null) = null
StringUtils.lowerCase("") = ""
StringUtils.lowerCase("aBc") = "abc"
Note: As described in the documentation for String.toLowerCase(),
the result of this method is affected by the current locale.
For platform-independent case transformations, the method lowerCase(String, Locale)
should be used with a specific locale (e.g. Locale.ENGLISH).
str - the String to lower case, may be nullnull if null String inputpublic static String lowerCase(String str, Locale locale)
Converts a String to lower case as per String.toLowerCase(Locale).
A null input String returns null.
StringUtils.lowerCase(null, Locale.ENGLISH) = null
StringUtils.lowerCase("", Locale.ENGLISH) = ""
StringUtils.lowerCase("aBc", Locale.ENGLISH) = "abc"
str - the String to lower case, may be nulllocale - the locale that defines the case transformation rules, must not be nullnull if null String inputpublic static String capitalize(String str)
Capitalizes a String changing the first letter to title case as
per Character.toTitleCase(char). No other letters are changed.
For a word based algorithm, see capitalize(String).
A null input String returns null.
StringUtils.capitalize(null) = null
StringUtils.capitalize("") = ""
StringUtils.capitalize("cat") = "Cat"
StringUtils.capitalize("cAt") = "CAt"
str - the String to capitalize, may be nullnull if null String inputWords.capitalize(String),
uncapitalize(String)public static String uncapitalize(String str)
Uncapitalizes a String changing the first letter to title case as
per Character.toLowerCase(char). No other letters are changed.
For a word based algorithm, see Words.uncapitalize(String).
A null input String returns null.
StringUtils.uncapitalize(null) = null
StringUtils.uncapitalize("") = ""
StringUtils.uncapitalize("Cat") = "cat"
StringUtils.uncapitalize("CAT") = "cAT"
str - the String to uncapitalize, may be nullnull if null String inputWords.uncapitalize(String),
capitalize(String)public static String swapCase(String str)
Swaps the case of a String changing upper and title case to lower case, and lower case to upper case.
For a word based algorithm, see Words.swapCase(String).
A null input String returns null.
StringUtils.swapCase(null) = null
StringUtils.swapCase("") = ""
StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
NOTE: This method changed in Lang version 2.0. It no longer performs a word based algorithm. If you only use ASCII, you will notice no change. That functionality is available in WordUtils.
str - the String to swap case, may be nullnull if null String inputpublic static String completingLength(String str, int expectedLength, char c, boolean left)
public static boolean isVowelLetter(char c)
public static boolean startsWithVowelLetter(String string)
public static String trim(String str)
Removes control characters (char <= 32) from both
ends of this String, handling null by returning
null.
The String is trimmed using String.trim().
Trim removes start and end characters <= 32.
To strip whitespace use strip(String).
To trim your choice of characters, use the
strip(String, String) methods.
StringUtils.trim(null) = null
StringUtils.trim("") = ""
StringUtils.trim(" ") = ""
StringUtils.trim("abc") = "abc"
StringUtils.trim(" abc ") = "abc"
str - the String to be trimmed, may be nullnull if null String inputpublic static String trimToNull(String str)
Removes control characters (char <= 32) from both
ends of this String returning null if the String is
empty ("") after the trim or if it is null.
The String is trimmed using String.trim().
Trim removes start and end characters <= 32.
To strip whitespace use stripToNull(String).
StringUtils.trimToNull(null) = null
StringUtils.trimToNull("") = null
StringUtils.trimToNull(" ") = null
StringUtils.trimToNull("abc") = "abc"
StringUtils.trimToNull(" abc ") = "abc"
str - the String to be trimmed, may be nullnull if only chars <= 32, empty or null String inputpublic static String trimToEmpty(String str)
Removes control characters (char <= 32) from both
ends of this String returning an empty String ("") if the String
is empty ("") after the trim or if it is null.
The String is trimmed using String.trim().
Trim removes start and end characters <= 32.
To strip whitespace use stripToEmpty(String).
StringUtils.trimToEmpty(null) = ""
StringUtils.trimToEmpty("") = ""
StringUtils.trimToEmpty(" ") = ""
StringUtils.trimToEmpty("abc") = "abc"
StringUtils.trimToEmpty(" abc ") = "abc"
str - the String to be trimmed, may be nullnull inputpublic static String truncate(String str, int maxWidth)
Truncates a String. This will turn "Now is the time for all good men" into "Now is the time for".
Specifically:
str is less than maxWidth characters
long, return it.substring(str, 0, maxWidth).maxWidth is less than 0, throw an
IllegalArgumentException.maxWidth.
StringUtils.truncate(null, 0) = null
StringUtils.truncate(null, 2) = null
StringUtils.truncate("", 4) = ""
StringUtils.truncate("abcdefg", 4) = "abcd"
StringUtils.truncate("abcdefg", 6) = "abcdef"
StringUtils.truncate("abcdefg", 7) = "abcdefg"
StringUtils.truncate("abcdefg", 8) = "abcdefg"
StringUtils.truncate("abcdefg", -1) = throws an IllegalArgumentException
str - the String to truncate, may be nullmaxWidth - maximum length of result String, must be positivenull if null String inputpublic static String truncate(String str, int offset, int maxWidth)
Truncates a String. This will turn "Now is the time for all good men" into "is the time for all".
Works like truncate(String, int), but allows you to specify
a "left edge" offset.
Specifically:
str is less than maxWidth characters
long, return it.substring(str, offset, maxWidth).maxWidth is less than 0, throw an
IllegalArgumentException.offset is less than 0, throw an
IllegalArgumentException.maxWidth.
StringUtils.truncate(null, 0, 0) = null
StringUtils.truncate(null, 2, 4) = null
StringUtils.truncate("", 0, 10) = ""
StringUtils.truncate("", 2, 10) = ""
StringUtils.truncate("abcdefghij", 0, 3) = "abc"
StringUtils.truncate("abcdefghij", 5, 6) = "fghij"
StringUtils.truncate("raspberry peach", 10, 15) = "peach"
StringUtils.truncate("abcdefghijklmno", 0, 10) = "abcdefghij"
StringUtils.truncate("abcdefghijklmno", -1, 10) = throws an IllegalArgumentException
StringUtils.truncate("abcdefghijklmno", Integer.MIN_VALUE, 10) = "abcdefghij"
StringUtils.truncate("abcdefghijklmno", Integer.MIN_VALUE, Integer.MAX_VALUE) = "abcdefghijklmno"
StringUtils.truncate("abcdefghijklmno", 0, Integer.MAX_VALUE) = "abcdefghijklmno"
StringUtils.truncate("abcdefghijklmno", 1, 10) = "bcdefghijk"
StringUtils.truncate("abcdefghijklmno", 2, 10) = "cdefghijkl"
StringUtils.truncate("abcdefghijklmno", 3, 10) = "defghijklm"
StringUtils.truncate("abcdefghijklmno", 4, 10) = "efghijklmn"
StringUtils.truncate("abcdefghijklmno", 5, 10) = "fghijklmno"
StringUtils.truncate("abcdefghijklmno", 5, 5) = "fghij"
StringUtils.truncate("abcdefghijklmno", 5, 3) = "fgh"
StringUtils.truncate("abcdefghijklmno", 10, 3) = "klm"
StringUtils.truncate("abcdefghijklmno", 10, Integer.MAX_VALUE) = "klmno"
StringUtils.truncate("abcdefghijklmno", 13, 1) = "n"
StringUtils.truncate("abcdefghijklmno", 13, Integer.MAX_VALUE) = "no"
StringUtils.truncate("abcdefghijklmno", 14, 1) = "o"
StringUtils.truncate("abcdefghijklmno", 14, Integer.MAX_VALUE) = "o"
StringUtils.truncate("abcdefghijklmno", 15, 1) = ""
StringUtils.truncate("abcdefghijklmno", 15, Integer.MAX_VALUE) = ""
StringUtils.truncate("abcdefghijklmno", Integer.MAX_VALUE, Integer.MAX_VALUE) = ""
StringUtils.truncate("abcdefghij", 3, -1) = throws an IllegalArgumentException
StringUtils.truncate("abcdefghij", -2, 4) = throws an IllegalArgumentException
str - the String to check, may be nulloffset - left edge of source StringmaxWidth - maximum length of result String, must be positivenull if null String inputpublic static String strip(String str)
Strips whitespace from the start and end of a String.
This is similar to trim(String) but removes whitespace.
Whitespace is defined by Character.isWhitespace(char).
A null input String returns null.
StringUtils.strip(null) = null
StringUtils.strip("") = ""
StringUtils.strip(" ") = ""
StringUtils.strip("abc") = "abc"
StringUtils.strip(" abc") = "abc"
StringUtils.strip("abc ") = "abc"
StringUtils.strip(" abc ") = "abc"
StringUtils.strip(" ab c ") = "ab c"
str - the String to remove whitespace from, may be nullnull if null String inputpublic static String stripToNull(String str)
Strips whitespace from the start and end of a String returning
null if the String is empty ("") after the strip.
This is similar to trimToNull(String) but removes whitespace.
Whitespace is defined by Character.isWhitespace(char).
StringUtils.stripToNull(null) = null
StringUtils.stripToNull("") = null
StringUtils.stripToNull(" ") = null
StringUtils.stripToNull("abc") = "abc"
StringUtils.stripToNull(" abc") = "abc"
StringUtils.stripToNull("abc ") = "abc"
StringUtils.stripToNull(" abc ") = "abc"
StringUtils.stripToNull(" ab c ") = "ab c"
str - the String to be stripped, may be nullnull if whitespace, empty or null String inputpublic static String stripToEmpty(String str)
Strips whitespace from the start and end of a String returning
an empty String if null input.
This is similar to trimToEmpty(String) but removes whitespace.
Whitespace is defined by Character.isWhitespace(char).
StringUtils.stripToEmpty(null) = ""
StringUtils.stripToEmpty("") = ""
StringUtils.stripToEmpty(" ") = ""
StringUtils.stripToEmpty("abc") = "abc"
StringUtils.stripToEmpty(" abc") = "abc"
StringUtils.stripToEmpty("abc ") = "abc"
StringUtils.stripToEmpty(" abc ") = "abc"
StringUtils.stripToEmpty(" ab c ") = "ab c"
str - the String to be stripped, may be nullnull inputpublic static String strip(String str, String stripChars)
Strips any of a set of characters from the start and end of a String.
This is similar to String.trim() but allows the characters
to be stripped to be controlled.
A null input String returns null.
An empty string ("") input returns the empty string.
If the stripChars String is null, whitespace is
stripped as defined by Character.isWhitespace(char).
Alternatively use strip(String).
StringUtils.strip(null, *) = null
StringUtils.strip("", *) = ""
StringUtils.strip("abc", null) = "abc"
StringUtils.strip(" abc", null) = "abc"
StringUtils.strip("abc ", null) = "abc"
StringUtils.strip(" abc ", null) = "abc"
StringUtils.strip(" abcyx", "xyz") = " abc"
str - the String to remove characters from, may be nullstripChars - the characters to remove, null treated as whitespacenull if null String inputpublic static String stripStart(String str, String stripChars)
Strips any of a set of characters from the start of a String.
A null input String returns null.
An empty string ("") input returns the empty string.
If the stripChars String is null, whitespace is
stripped as defined by Character.isWhitespace(char).
StringUtils.stripStart(null, *) = null
StringUtils.stripStart("", *) = ""
StringUtils.stripStart("abc", "") = "abc"
StringUtils.stripStart("abc", null) = "abc"
StringUtils.stripStart(" abc", null) = "abc"
StringUtils.stripStart("abc ", null) = "abc "
StringUtils.stripStart(" abc ", null) = "abc "
StringUtils.stripStart("yxabc ", "xyz") = "abc "
str - the String to remove characters from, may be nullstripChars - the characters to remove, null treated as whitespacenull if null String inputpublic static String stripEnd(String str, String stripChars)
Strips any of a set of characters from the end of a String.
A null input String returns null.
An empty string ("") input returns the empty string.
If the stripChars String is null, whitespace is
stripped as defined by Character.isWhitespace(char).
StringUtils.stripEnd(null, *) = null
StringUtils.stripEnd("", *) = ""
StringUtils.stripEnd("abc", "") = "abc"
StringUtils.stripEnd("abc", null) = "abc"
StringUtils.stripEnd(" abc", null) = " abc"
StringUtils.stripEnd("abc ", null) = "abc"
StringUtils.stripEnd(" abc ", null) = " abc"
StringUtils.stripEnd(" abcyx", "xyz") = " abc"
StringUtils.stripEnd("120.00", ".0") = "12"
str - the String to remove characters from, may be nullstripChars - the set of characters to remove, null treated as whitespacenull if null String inputpublic static String[] stripAll(String... strs)
Strips whitespace from the start and end of every String in an array.
Whitespace is defined by Character.isWhitespace(char).
A new array is returned each time, except for length zero.
A null array will return null.
An empty array will return itself.
A null array entry will be ignored.
Strings.stripAll(null) = null Strings.stripAll([]) = [] Strings.stripAll(["abc", " abc"]) = ["abc", "abc"] Strings.stripAll(["abc ", null]) = ["abc", null]
strs - the array to remove whitespace from, may be nullnull if null array inputpublic static String[] stripAll(String[] strs, String stripChars)
Strips any of a set of characters from the start and end of every String in an array.
Whitespace is defined by Character.isWhitespace(char).
A new array is returned each time, except for length zero.
A null array will return null.
An empty array will return itself.
A null array entry will be ignored.
A null stripChars will strip whitespace as defined by
Character.isWhitespace(char).
StringUtils.stripAll(null, *) = null StringUtils.stripAll([], *) = [] StringUtils.stripAll(["abc", " abc"], null) = ["abc", "abc"] StringUtils.stripAll(["abc ", null], null) = ["abc", null] StringUtils.stripAll(["abc ", null], "yz") = ["abc ", null] StringUtils.stripAll(["yabcz", null], "yz") = ["abc", null]
strs - the array to remove characters from, may be nullstripChars - the characters to remove, null treated as whitespacenull if null array inputpublic static String stripAccents(String input)
Removes diacritics (~= accents) from a string. The case will not be altered.
For instance, 'à' will be replaced by 'a'.
Note that ligatures will be left as is.
StringUtils.stripAccents(null) = null
StringUtils.stripAccents("") = ""
StringUtils.stripAccents("control") = "control"
StringUtils.stripAccents("éclair") = "eclair"
input - String to be strippedpublic static boolean equals(CharSequence cs1, CharSequence cs2)
Compares two CharSequences, returning true if they represent
equal sequences of characters.
nulls are handled without exceptions. Two null
references are considered to be equal. The comparison is case sensitive.
StringUtils.equals(null, null) = true
StringUtils.equals(null, "abc") = false
StringUtils.equals("abc", null) = false
StringUtils.equals("abc", "abc") = true
StringUtils.equals("abc", "ABC") = false
cs1 - the first CharSequence, may be nullcs2 - the second CharSequence, may be nulltrue if the CharSequences are equal (case-sensitive), or both nullObject.equals(Object),
Changed signature from equals(String, String) to equals(CharSequence, CharSequence)public static boolean equalsIgnoreCase(CharSequence cs1, CharSequence cs2)
Compares two CharSequences, returning true if they represent
equal sequences of characters, ignoring case.
nulls are handled without exceptions. Two null
references are considered equal. The comparison is case insensitive.
StringUtils.equalsIgnoreCase(null, null) = true
StringUtils.equalsIgnoreCase(null, "abc") = false
StringUtils.equalsIgnoreCase("abc", null) = false
StringUtils.equalsIgnoreCase("abc", "abc") = true
StringUtils.equalsIgnoreCase("abc", "ABC") = true
cs1 - the first CharSequence, may be nullcs2 - the second CharSequence, may be nulltrue if the CharSequences are equal (case-insensitive), or both nullChanged signature from equalsIgnoreCase(String, String) to equalsIgnoreCase(CharSequence, CharSequence)public static int compare(String str1, String str2)
Compare two Strings lexicographically, as per String.compareTo(String), returning :
int = 0, if str1 is equal to str2 (or both null)int < 0, if str1 is less than str2int > 0, if str1 is greater than str2This is a null safe version of :
str1.compareTo(str2)
null value is considered less than non-null value.
Two null references are considered equal.
StringUtils.compare(null, null) = 0
StringUtils.compare(null , "a") < 0
StringUtils.compare("a", null) > 0
StringUtils.compare("abc", "abc") = 0
StringUtils.compare("a", "b") < 0
StringUtils.compare("b", "a") > 0
StringUtils.compare("a", "B") > 0
StringUtils.compare("ab", "abc") < 0
str1 - the String to compare fromstr2 - the String to compare tostr1 is respectively less, equal or greater than str2compare(String, String, boolean),
String.compareTo(String)public static int compare(String str1, String str2, boolean nullIsLess)
Compare two Strings lexicographically, as per String.compareTo(String), returning :
int = 0, if str1 is equal to str2 (or both null)int < 0, if str1 is less than str2int > 0, if str1 is greater than str2This is a null safe version of :
str1.compareTo(str2)
null inputs are handled according to the nullIsLess parameter.
Two null references are considered equal.
StringUtils.compare(null, null, *) = 0
StringUtils.compare(null , "a", true) < 0
StringUtils.compare(null , "a", false) > 0
StringUtils.compare("a", null, true) > 0
StringUtils.compare("a", null, false) < 0
StringUtils.compare("abc", "abc", *) = 0
StringUtils.compare("a", "b", *) < 0
StringUtils.compare("b", "a", *) > 0
StringUtils.compare("a", "B", *) > 0
StringUtils.compare("ab", "abc", *) < 0
str1 - the String to compare fromstr2 - the String to compare tonullIsLess - whether consider null value less than non-null valuestr1 is respectively less, equal ou greater than str2String.compareTo(String)public static int compareIgnoreCase(String str1, String str2)
Compare two Strings lexicographically, ignoring case differences,
as per String.compareToIgnoreCase(String), returning :
int = 0, if str1 is equal to str2 (or both null)int < 0, if str1 is less than str2int > 0, if str1 is greater than str2This is a null safe version of :
str1.compareToIgnoreCase(str2)
null value is considered less than non-null value.
Two null references are considered equal.
Comparison is case insensitive.
StringUtils.compareIgnoreCase(null, null) = 0
StringUtils.compareIgnoreCase(null , "a") < 0
StringUtils.compareIgnoreCase("a", null) > 0
StringUtils.compareIgnoreCase("abc", "abc") = 0
StringUtils.compareIgnoreCase("abc", "ABC") = 0
StringUtils.compareIgnoreCase("a", "b") < 0
StringUtils.compareIgnoreCase("b", "a") > 0
StringUtils.compareIgnoreCase("a", "B") < 0
StringUtils.compareIgnoreCase("A", "b") < 0
StringUtils.compareIgnoreCase("ab", "ABC") < 0
str1 - the String to compare fromstr2 - the String to compare tostr1 is respectively less, equal ou greater than str2,
ignoring case differences.compareIgnoreCase(String, String, boolean),
String.compareToIgnoreCase(String)public static int compareIgnoreCase(String str1, String str2, boolean nullIsLess)
Compare two Strings lexicographically, ignoring case differences,
as per String.compareToIgnoreCase(String), returning :
int = 0, if str1 is equal to str2 (or both null)int < 0, if str1 is less than str2int > 0, if str1 is greater than str2This is a null safe version of :
str1.compareToIgnoreCase(str2)
null inputs are handled according to the nullIsLess parameter.
Two null references are considered equal.
Comparison is case insensitive.
StringUtils.compareIgnoreCase(null, null, *) = 0
StringUtils.compareIgnoreCase(null , "a", true) < 0
StringUtils.compareIgnoreCase(null , "a", false) > 0
StringUtils.compareIgnoreCase("a", null, true) > 0
StringUtils.compareIgnoreCase("a", null, false) < 0
StringUtils.compareIgnoreCase("abc", "abc", *) = 0
StringUtils.compareIgnoreCase("abc", "ABC", *) = 0
StringUtils.compareIgnoreCase("a", "b", *) < 0
StringUtils.compareIgnoreCase("b", "a", *) > 0
StringUtils.compareIgnoreCase("a", "B", *) < 0
StringUtils.compareIgnoreCase("A", "b", *) < 0
StringUtils.compareIgnoreCase("ab", "abc", *) < 0
str1 - the String to compare fromstr2 - the String to compare tonullIsLess - whether consider null value less than non-null valuestr1 is respectively less, equal ou greater than str2,
ignoring case differences.String.compareToIgnoreCase(String)public static boolean equalsAny(CharSequence string, CharSequence... searchStrings)
Compares given string to a CharSequences vararg of searchStrings,
returning true if the string is equal to any of the searchStrings.
StringUtils.equalsAny(null, (CharSequence[]) null) = false
StringUtils.equalsAny(null, null, null) = true
StringUtils.equalsAny(null, "abc", "def") = false
StringUtils.equalsAny("abc", null, "def") = false
StringUtils.equalsAny("abc", "abc", "def") = true
StringUtils.equalsAny("abc", "ABC", "DEF") = false
string - to compare, may be null.searchStrings - a vararg of strings, may be null.true if the string is equal (case-sensitive) to any other element of searchStrings;
false if searchStrings is null or contains no matches.public static boolean equalsAnyIgnoreCase(CharSequence string, CharSequence... searchStrings)
Compares given string to a CharSequences vararg of searchStrings,
returning true if the string is equal to any of the searchStrings, ignoring case.
StringUtils.equalsAnyIgnoreCase(null, (CharSequence[]) null) = false
StringUtils.equalsAnyIgnoreCase(null, null, null) = true
StringUtils.equalsAnyIgnoreCase(null, "abc", "def") = false
StringUtils.equalsAnyIgnoreCase("abc", null, "def") = false
StringUtils.equalsAnyIgnoreCase("abc", "abc", "def") = true
StringUtils.equalsAnyIgnoreCase("abc", "ABC", "DEF") = true
string - to compare, may be null.searchStrings - a vararg of strings, may be null.true if the string is equal (case-insensitive) to any other element of searchStrings;
false if searchStrings is null or contains no matches.public static int indexOf(CharSequence seq, int searchChar)
seq of the first occurrence of
the specified character. If a character with value
searchChar occurs in the character sequence represented by
seq CharSequence object, then the index (in Unicode
code units) of the first such occurrence is returned. For
values of searchChar in the range from 0 to 0xFFFF
(inclusive), this is the smallest value k such that:
is true. For other values ofthis.charAt(k) == searchChar
searchChar, it is the
smallest value k such that:
is true. In either case, if no such character occurs inthis.codePointAt(k) == searchChar
seq,
then INDEX_NOT_FOUND (-1) is returned.
Furthermore, a null or empty ("") CharSequence will
return INDEX_NOT_FOUND (-1).
StringUtils.indexOf(null, *) = -1
StringUtils.indexOf("", *) = -1
StringUtils.indexOf("aabaabaa", 'a') = 0
StringUtils.indexOf("aabaabaa", 'b') = 2
seq - the CharSequence to check, may be nullsearchChar - the character to findnull string inputpublic static int indexOf(CharSequence seq, int searchChar, int startPos)
seq of the first occurrence of the
specified character, starting the search at the specified index.
If a character with value searchChar occurs in the
character sequence represented by the seq CharSequence
object at an index no smaller than startPos, then
the index of the first such occurrence is returned. For values
of searchChar in the range from 0 to 0xFFFF (inclusive),
this is the smallest value k such that:
is true. For other values of(this.charAt(k) == searchChar) && (k >= startPos)
searchChar, it is the
smallest value k such that:
is true. In either case, if no such character occurs in(this.codePointAt(k) == searchChar) && (k >= startPos)
seq
at or after position startPos, then
-1 is returned.
There is no restriction on the value of startPos. If it
is negative, it has the same effect as if it were zero: this entire
string may be searched. If it is greater than the length of this
string, it has the same effect as if it were equal to the length of
this string: (INDEX_NOT_FOUND) -1 is returned. Furthermore, a
null or empty ("") CharSequence will
return (INDEX_NOT_FOUND) -1.
All indices are specified in char values
(Unicode code units).
StringUtils.indexOf(null, *, *) = -1
StringUtils.indexOf("", *, *) = -1
StringUtils.indexOf("aabaabaa", 'b', 0) = 2
StringUtils.indexOf("aabaabaa", 'b', 3) = 5
StringUtils.indexOf("aabaabaa", 'b', 9) = -1
StringUtils.indexOf("aabaabaa", 'b', -1) = 2
seq - the CharSequence to check, may be nullsearchChar - the character to findstartPos - the start position, negative treated as zeronull string inputpublic static int indexOf(CharSequence seq, CharSequence searchSeq)
Finds the first index within a CharSequence, handling null.
This method uses String.indexOf(String, int) if possible.
A null CharSequence will return -1.
StringUtils.indexOf(null, *) = -1
StringUtils.indexOf(*, null) = -1
StringUtils.indexOf("", "") = 0
StringUtils.indexOf("", *) = -1 (except when * = "")
StringUtils.indexOf("aabaabaa", "a") = 0
StringUtils.indexOf("aabaabaa", "b") = 2
StringUtils.indexOf("aabaabaa", "ab") = 1
StringUtils.indexOf("aabaabaa", "") = 0
seq - the CharSequence to check, may be nullsearchSeq - the CharSequence to find, may be nullnull string input
Changed signature from indexOf(String, String) to indexOf(CharSequence, CharSequence)public static int indexOf(CharSequence seq, CharSequence searchSeq, int startPos)
Finds the first index within a CharSequence, handling null.
This method uses String.indexOf(String, int) if possible.
A null CharSequence will return -1.
A negative start position is treated as zero.
An empty ("") search CharSequence always matches.
A start position greater than the string length only matches
an empty search CharSequence.
StringUtils.indexOf(null, *, *) = -1
StringUtils.indexOf(*, null, *) = -1
StringUtils.indexOf("", "", 0) = 0
StringUtils.indexOf("", *, 0) = -1 (except when * = "")
StringUtils.indexOf("aabaabaa", "a", 0) = 0
StringUtils.indexOf("aabaabaa", "b", 0) = 2
StringUtils.indexOf("aabaabaa", "ab", 0) = 1
StringUtils.indexOf("aabaabaa", "b", 3) = 5
StringUtils.indexOf("aabaabaa", "b", 9) = -1
StringUtils.indexOf("aabaabaa", "b", -1) = 2
StringUtils.indexOf("aabaabaa", "", 2) = 2
StringUtils.indexOf("abc", "", 9) = 3
seq - the CharSequence to check, may be nullsearchSeq - the CharSequence to find, may be nullstartPos - the start position, negative treated as zeronull string input
Changed signature from indexOf(String, String, int) to indexOf(CharSequence, CharSequence, int)public static CharSequence subSequence(CharSequence cs, int start)
Returns a new CharSequence that is a subsequence of this
sequence starting with the char value at the specified index.
This provides the CharSequence equivalent to String.substring(int).
The length (in char) of the returned sequence is length() - start,
so if start == end then an empty sequence is returned.
cs - the specified subsequence, null returns nullstart - the start index, inclusive, validIndexOutOfBoundsException - if start is negative or if
start is greater than length()Copyright © 2020. All rights reserved.