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 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 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 boolean |
isBlank(String str)
judge a string has some whitespace at most
|
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)
Checks if the CharSequence contains only Unicode digits.
|
static boolean |
isVowelLetter(char c) |
static String |
join(String separator,
Iterable objects) |
static String |
join(String separator,
Iterator objects)
append all objects with the specified separator
|
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 boolean |
regionMatches(CharSequence cs,
boolean ignoreCase,
int thisStart,
CharSequence substring,
int start,
int length)
Green implementation of regionMatches.
|
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 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 |
trimOrEmpty(String str) |
static String |
truncate(String string,
int length)
Get substring from 0 to a specified length
equals: string.substring(0, length)
|
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 ls)
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) |
public static final int INDEX_NOT_FOUND
public static boolean isEmpty(String str)
str - the specified stringpublic static boolean isNotEmpty(String str)
public static boolean isBlank(String str)
str - the specified stringpublic static boolean isNotBlank(String str)
public static String truncate(@NonNull String string, int length)
string - a string will be truncatedlength - new string's lengthpublic static String join(@NonNull String separator, @Nullable Iterator objects)
separator - the specified separatorobjects - the dbjects that will be appendpublic 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.NullPointerException instead of UnsupportedEncodingExceptionpublic 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.NullPointerException instead of UnsupportedEncodingExceptionpublic 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 ls)
s - a not null Stringls - 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)
Checks if the CharSequence contains only Unicode digits. A decimal point is not a Unicode digit and returns false.
null will return false.
An empty CharSequence (length()=0) will return false.
Note that the method does not allow for a leading sign, either positive or negative. Also, if a String passes the numeric test, it may still generate a NumberFormatException when parsed by Integer.parseInt or Long.parseLong, e.g. if the value is outside the range for int or long respectively.
StringUtils.isNumeric(null) = false
StringUtils.isNumeric("") = false
StringUtils.isNumeric(" ") = false
StringUtils.isNumeric("123") = true
StringUtils.isNumeric("12 3") = false
StringUtils.isNumeric("ab2c") = false
StringUtils.isNumeric("12-3") = false
StringUtils.isNumeric("12.3") = false
StringUtils.isNumeric("-123") = false
StringUtils.isNumeric("+123") = false
cs - the CharSequence to check, may be nulltrue if only contains digits, and is non-null
Changed signature from isNumeric(String) to isNumeric(CharSequence)
Changed "" to return false and not truepublic static boolean regionMatches(CharSequence cs, boolean ignoreCase, int thisStart, CharSequence substring, int start, int length)
cs - the CharSequence to be processedignoreCase - whether or not to be case insensitivethisStart - the index to start on the cs CharSequencesubstring - the CharSequence to be looked forstart - the index to start on the substring CharSequencelength - character length of the regionpublic 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)
Copyright © 2019. All rights reserved.