public class StringUtils extends Object
Constructor and Description |
---|
StringUtils() |
Modifier and Type | Method and Description |
---|---|
static String |
center(String str,
int size) |
static String |
center(String str,
int size,
char padChar) |
static boolean |
contains(CharSequence seq,
CharSequence searchSeq)
Checks if CharSequence contains a search CharSequence, handling
null . |
static boolean |
contains(CharSequence seq,
int searchChar)
Checks if CharSequence contains a search character, handling
null . |
static boolean |
containsIgnoreCase(CharSequence str,
CharSequence searchStr)
Checks if CharSequence contains a search CharSequence irrespective of case,
handling
null . |
static int |
count(String source,
char c) |
static int |
count(String source,
char c,
int start) |
static int |
count(String source,
String substr) |
static int |
count(String source,
String substr,
int start) |
static String |
defaultIfBlank(String s,
String defaultStr)
Returns either the passed in String, or if the String is
whitespace, empty ("") or
null , the value of defaultStr . |
static String |
defaultIfEmpty(String s,
String defaultStr)
Returns either the passed in String, or if the String is
empty or
null , the value of defaultStr . |
static String |
defaultIfNull(String s,
String defaultStr)
Returns either the passed in String, or if the String is
null ,
the value of defaultStr . |
static String |
deleteWhitespace(String s)
Deletes all whitespaces from a String as defined by
Character.isWhitespace(char) . |
static boolean |
endsWith(String s,
String suffix)
Check if a String ends with a specified suffix.
|
static boolean |
endsWithChar(String s,
char c) |
static boolean |
endsWithIgnoreCase(String s,
String suffix)
Case insensitive check if a String ends with a specified suffix.
|
static int |
endsWithOne(String src,
String... dest) |
static int |
endsWithOneIgnoreCase(String s,
String... dest) |
static boolean |
equals(String s1,
String s2)
Compares two CharSequences, returning
true if they represent
equal sequences of characters. |
static boolean |
equalsIgnoreCase(String s1,
String s2)
Compares two CharSequences, returning
true if they represent
equal sequences of characters, ignoring case. |
static String |
firstCharToLowerCase(String str)
首字母变小写
|
static String |
firstCharToUpperCase(String str)
首字母变大写
|
static byte[] |
getBytes(String s,
String charsetName) |
static String |
getCRC(InputStream in)
计算CRC摘要,8位十六进制数
|
static String |
getCRC(String s)
计算CRC摘要,8位十六进制数
|
static byte[] |
getUtf8Bytes(String s) |
static int[] |
indexOf(String s,
char[] c)
Finds the very first index of a substring from the specified array.
|
static int[] |
indexOf(String s,
char[] c,
int start)
Finds the very first index of a char from the specified array.
|
static int |
indexOf(String s,
char c,
int startIndex,
int endIndex)
Finds the first occurrence of a character in the given source but within limited range (start, end].
|
static int[] |
indexOf(String s,
String[] arr)
Finds the very first index of a substring from the specified array.
|
static int[] |
indexOf(String s,
String[] arr,
int start)
Finds the very first index of a substring from the specified array.
|
static int |
indexOf(String s,
String substr,
int startIndex,
int endIndex)
Finds first occurrence of a substring in the given source but within limited range [start, end).
|
static int |
indexOfIgnoreCase(String s,
char c,
int startIndex,
int endIndex)
Finds the first occurrence of a character in the given source but within limited range (start, end].
|
static int |
indexOfIgnoreCase(String s,
String substr)
Finds first index of a substring in the given source string with ignored case.
|
static int[] |
indexOfIgnoreCase(String s,
String[] arr)
Finds the very first index of a substring from the specified array.
|
static int[] |
indexOfIgnoreCase(String s,
String[] arr,
int start)
Finds the very first index of a substring from the specified array.
|
static int |
indexOfIgnoreCase(String s,
String substr,
int startIndex)
Finds first index of a substring in the given source string with ignored
case.
|
static int |
indexOfIgnoreCase(String s,
String substr,
int startIndex,
int endIndex)
Finds first index of a substring in the given source string and range with
ignored case.
|
static boolean |
isBlank(String s)
Checks if a String is whitespace, empty ("") or null.
|
static boolean |
isEmpty(CharSequence cs) |
static boolean |
isEmpty(String s)
Checks if a String is empty ("") or null.
|
static boolean |
isNotBlank(String s)
Checks if a String is not empty (""), not null and not whitespace
only.
|
static boolean |
isNotEmpty(String s)
Checks if a String is not empty ("") and not null.
|
static String |
join(Iterable<?> elements,
String separator) |
static String |
join(Iterator<?> elements,
String separator) |
static String |
join(Object[] elements,
String separator) |
static String |
join(String... parts) |
static String |
join(String[] stringArray,
String separator) |
static int[] |
lastIndexOf(String s,
char[] c)
Finds the very last index of a substring from the specified array.
|
static int[] |
lastIndexOf(String s,
char[] c,
int fromIndex)
Finds the very last index of a substring from the specified array.
|
static int |
lastIndexOf(String s,
char c,
int startIndex,
int endIndex)
Finds last index of a character in the given source string in specified range [end, start]
|
static int[] |
lastIndexOf(String s,
String[] arr)
Finds the very last index of a substring from the specified array.
|
static int[] |
lastIndexOf(String s,
String[] arr,
int fromIndex)
Finds the very last index of a substring from the specified array.
|
static int |
lastIndexOf(String s,
String sub,
int startIndex,
int endIndex)
Finds last index of a substring in the given source string in specified range [end, start]
See
indexOf(String, String, int, int) for details about the speed. |
static int |
lastIndexOfIgnoreCase(String s,
char c,
int startIndex,
int endIndex)
Finds last index of a character in the given source string in specified range [end, start]
|
static int |
lastIndexOfIgnoreCase(String s,
String substr)
Finds last index of a substring in the given source string with ignored
case.
|
static int[] |
lastIndexOfIgnoreCase(String s,
String[] arr)
Finds the very last index of a substring from the specified array.
|
static int[] |
lastIndexOfIgnoreCase(String s,
String[] arr,
int fromIndex)
Finds the very last index of a substring from the specified array.
|
static int |
lastIndexOfIgnoreCase(String s,
String substr,
int startIndex)
Finds last index of a substring in the given source string with ignored
case.
|
static int |
lastIndexOfIgnoreCase(String s,
String sub,
int startIndex,
int endIndex)
Finds last index of a substring in the given source string with ignored
case in specified range.
|
static String |
left(String s,
int len)
Gets the leftmost
len characters of a String. |
static String |
leftPad(String str,
int size) |
static String |
leftPad(String str,
int size,
char padChar) |
static String |
leftPad(String str,
int size,
String padStr) |
static String |
lowerCase(String str)
Converts a String to lower case as per
String.toLowerCase() . |
static String |
lowerCase(String str,
Locale locale)
Converts a String to lower case as per
String.toLowerCase(Locale) . |
static String |
mid(String s,
int pos,
int len)
Gets
len characters from the middle of a String. |
static String |
prefix(String s,
String prefix) |
static String |
randomString()
产生8位的随机数字
|
static String |
remove(String s,
char remove)
Removes all occurrences of a character from within the source string.
|
static String |
remove(String s,
String remove)
Removes all occurrences of a substring from within the source string.
|
static String |
removeChars(String s,
char... chars)
Removes set of characters from string.
|
static String |
removeChars(String s,
String chars)
Removes all characters contained in provided string.
|
static String |
removeEnd(String s,
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 s,
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 |
removeStart(String s,
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 s,
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 s,
int repeat)
Repeat a String
repeat times to form a new String. |
static String |
repeat(String s,
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 |
reverse(String s) |
static String |
right(String s,
int len)
Gets the rightmost
len characters of a String. |
static String |
rightPad(String str,
int size) |
static String |
rightPad(String str,
int size,
char padChar) |
static String |
rightPad(String str,
int size,
String padStr) |
static String[] |
split(String str,
char delimiter) |
static String[] |
split(String str,
String delimiter) |
static String[] |
splitChars(String str,
char... delimiters) |
static String[] |
splitChars(String str,
String delimiters) |
static String[] |
splitCSV(String str)
按逗号分隔,两边加引号的不分割(CSV 规则).
|
static boolean |
startsWith(String s,
String prefix)
Check if a String starts with a specified prefix.
|
static boolean |
startsWithChar(String s,
char c) |
static boolean |
startsWithIgnoreCase(String s,
String prefix)
Case insensitive check if a String starts with a specified prefix.
|
static int |
startsWithOne(String s,
String... dest) |
static int |
startsWithOneIgnoreCase(String s,
String... dest) |
static String |
substring(String s,
int start)
Gets a substring from the specified String avoiding exceptions.
|
static String |
substring(String s,
int start,
int end)
Gets a substring from the specified String avoiding exceptions.
|
static String |
substringAfter(String s,
String separator)
Gets the substring after the first occurrence of a separator.
|
static String |
substringAfterLast(String s,
String separator)
Gets the substring after the last occurrence of a separator.
|
static String |
substringAfterLastIfExist(String source,
String keyword)
返回字串,如果查找的字串不存在则返回全部
和substringAfterLast方法不同,substringAfterLast方法在查找不到时返回空串 |
static String |
substringBefore(String s,
String separator)
Gets the substring before the first occurrence of a separator.
|
static String |
substringBeforeLast(String s,
String separator)
Gets the substring before the last occurrence of a separator.
|
static String |
substringBetween(String s,
String tag)
Gets the String that is nested in between two instances of the same
String.
|
static String |
substringBetween(String s,
String open,
String close)
Gets the String that is nested in between two Strings.
|
static String |
suffix(String s,
String suffix) |
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 |
toCamelCase(String stringWithUnderline) |
static String |
trim(String s)
Removes control characters (char <= 32) from both ends of this String,
handling
null by returning null . |
static String[] |
trim(String[] ss) |
static String |
trimToEmpty(String s)
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 s)
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 |
upperCase(String str)
Converts a String to upper case as per
String.toUpperCase() . |
static String |
upperCase(String str,
Locale locale)
Converts a String to upper case as per
String.toUpperCase(Locale) . |
static String |
uuid32()
返回32位uuid
|
static String |
uuid36()
返回36位uuid
|
public static String uuid32()
public static String uuid36()
public static final String randomString()
public static String getCRC(InputStream in)
public static String substringAfterLastIfExist(String source, String keyword)
source
- 源字符串keyword
- 查找字public static boolean isEmpty(CharSequence cs)
public static boolean isEmpty(String s)
Checks if a String is empty ("") or null.
StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("bob") = false StringUtils.isEmpty(" bob ") = false
public static boolean isNotEmpty(String s)
Checks if a String is not empty ("") and not null.
StringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = false StringUtils.isNotEmpty(" ") = true StringUtils.isNotEmpty("bob") = true StringUtils.isNotEmpty(" bob ") = true
public static boolean isBlank(String s)
Checks if a String is whitespace, empty ("") or null.
StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true StringUtils.isBlank("bob") = false StringUtils.isBlank(" bob ") = false
public static boolean isNotBlank(String s)
Checks if a String is not empty (""), not null and not whitespace only.
StringUtils.isNotBlank(null) = false StringUtils.isNotBlank("") = false StringUtils.isNotBlank(" ") = false StringUtils.isNotBlank("bob") = true StringUtils.isNotBlank(" bob ") = true
public static byte[] getUtf8Bytes(String s)
public static String trim(String s)
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.
StringUtils.trim(null) = null StringUtils.trim("") = "" StringUtils.trim(" ") = "" StringUtils.trim("abc") = "abc" StringUtils.trim(" abc ") = "abc"
public static String trimToNull(String s)
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.
StringUtils.trimToNull(null) = null StringUtils.trimToNull("") = null StringUtils.trimToNull(" ") = null StringUtils.trimToNull("abc") = "abc" StringUtils.trimToNull(" abc ") = "abc"
public static String trimToEmpty(String s)
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.
StringUtils.trimToEmpty(null) = "" StringUtils.trimToEmpty("") = "" StringUtils.trimToEmpty(" ") = "" StringUtils.trimToEmpty("abc") = "abc" StringUtils.trimToEmpty(" abc ") = "abc"
public static String defaultIfNull(String s, String defaultStr)
Returns either the passed in String, or if the String is null
,
the value of defaultStr
.
StringUtils.defaultIfNull(null, "NULL") = "NULL" StringUtils.defaultIfNull("", "NULL") = "" StringUtils.defaultIfNull("bat", "NULL") = "bat"
public static String defaultIfEmpty(String s, String defaultStr)
Returns either the passed in String, or if the String is
empty or null
, the value of defaultStr
.
StringUtils.defaultIfEmpty(null, "NULL") = "NULL" StringUtils.defaultIfEmpty("", "NULL") = "NULL" StringUtils.defaultIfEmpty(" ", "NULL") = " " StringUtils.defaultIfEmpty("bat", "NULL") = "bat" StringUtils.defaultIfEmpty("", null) = null
public static String defaultIfBlank(String s, String defaultStr)
Returns either the passed in String, or if the String is
whitespace, empty ("") or null
, the value of defaultStr
.
StringUtils.defaultIfBlank(null, "NULL") = "NULL" StringUtils.defaultIfBlank("", "NULL") = "NULL" StringUtils.defaultIfBlank(" ", "NULL") = "NULL" StringUtils.defaultIfBlank("bat", "NULL") = "bat" StringUtils.defaultIfBlank("", null) = null
public static boolean equals(String s1, String s2)
Compares two CharSequences, returning true
if they represent
equal sequences of characters.
null
s 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
public static boolean equalsIgnoreCase(String s1, String s2)
Compares two CharSequences, returning true
if they represent
equal sequences of characters, ignoring case.
null
s are handled without exceptions. Two null
references
are considered equal. 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
public static boolean startsWith(String s, String prefix)
Check if a String starts with a specified prefix.
null
s are handled without exceptions. Two null
references
are considered to be equal. The comparison is case sensitive.
StringUtils.startsWith(null, null) = true StringUtils.startsWith(null, "abc") = false StringUtils.startsWith("abcdef", null) = false StringUtils.startsWith("abcdef", "abc") = true StringUtils.startsWith("ABCDEF", "abc") = false
s
- the String to check, may be nullprefix
- the prefix to find, may be nulltrue
if the String starts with the prefix, case
sensitive, or both null
String.startsWith(String)
public static boolean startsWithIgnoreCase(String s, String prefix)
Case insensitive check if a String starts with a specified prefix.
null
s 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
String.startsWith(String)
public static boolean startsWithChar(String s, char c)
public static boolean endsWith(String s, String suffix)
Check if a String ends with a specified suffix.
null
s 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
String.endsWith(String)
public static boolean endsWithIgnoreCase(String s, String suffix)
Case insensitive check if a String ends with a specified suffix.
null
s 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
String.endsWith(String)
public static boolean endsWithChar(String s, char c)
public static int indexOf(String s, String substr, int startIndex, int endIndex)
String.indexOf(String, int)
is much faster (since it uses char[] value directly) and should be used when no range is needed.s
- source string for examinationsubstr
- substring to findstartIndex
- starting indexendIndex
- ending indexpublic static int indexOf(String s, char c, int startIndex, int endIndex)
public static int indexOfIgnoreCase(String s, char c, int startIndex, int endIndex)
public static int indexOfIgnoreCase(String s, String substr)
s
- source string for examinationsubstr
- substring to findindexOfIgnoreCase(String, String, int)
public static int indexOfIgnoreCase(String s, String substr, int startIndex)
s
- source string for examinationsubstr
- substring to findstartIndex
- starting index from where search beginspublic static int indexOfIgnoreCase(String s, String substr, int startIndex, int endIndex)
s
- source string for examinationsubstr
- substring to findstartIndex
- starting index from where search beginsendIndex
- endint indexindexOfIgnoreCase(String, String, int)
public static int[] indexOf(String s, String[] arr)
null
if
noting found.s
- source stringarr
- string arraypublic static int[] indexOf(String s, String[] arr, int start)
null
if noting found.s
- source stringarr
- string arraystart
- starting positionpublic static int[] indexOf(String s, char[] c)
null
if
noting found.s
- source stringc
- char arraypublic static int[] indexOf(String s, char[] c, int start)
null
if noting found.s
- source stringc
- char arraystart
- starting positionpublic static int[] indexOfIgnoreCase(String s, String[] arr)
null
if noting found.s
- source stringarr
- string arraypublic static int[] indexOfIgnoreCase(String s, String[] arr, int start)
null
if noting found.s
- source stringarr
- string arraystart
- starting positionpublic static int lastIndexOfIgnoreCase(String s, String substr)
s
- source stringsubstr
- substring to findindexOfIgnoreCase(String, String, int)
,
lastIndexOfIgnoreCase(String, String, int)
public static int lastIndexOfIgnoreCase(String s, String substr, int startIndex)
s
- source string for examinationsubstr
- substring to findstartIndex
- starting index from where search beginsindexOfIgnoreCase(String, String, int)
public static int lastIndexOfIgnoreCase(String s, String sub, int startIndex, int endIndex)
s
- source to examinesub
- substring to findstartIndex
- starting indexendIndex
- end indexpublic static int lastIndexOf(String s, String sub, int startIndex, int endIndex)
indexOf(String, String, int, int)
for details about the speed.s
- source to examinesub
- substring to findstartIndex
- starting indexendIndex
- end indexpublic static int lastIndexOf(String s, char c, int startIndex, int endIndex)
public static int lastIndexOfIgnoreCase(String s, char c, int startIndex, int endIndex)
public static int[] lastIndexOf(String s, String[] arr)
null
if noting found.s
- source stringarr
- string arraypublic static int[] lastIndexOf(String s, String[] arr, int fromIndex)
null
if noting found.s
- source stringarr
- string arrayfromIndex
- starting positionpublic static int[] lastIndexOf(String s, char[] c)
null
if noting found.s
- source stringc
- char arraypublic static int[] lastIndexOf(String s, char[] c, int fromIndex)
null
if noting found.s
- source stringc
- char arrayfromIndex
- starting positionpublic static int[] lastIndexOfIgnoreCase(String s, String[] arr)
null
if noting found.s
- source stringarr
- string arraypublic static int[] lastIndexOfIgnoreCase(String s, String[] arr, int fromIndex)
null
if noting found.s
- source stringarr
- string arrayfromIndex
- starting positionpublic static String substring(String s, int start)
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start n
characters from
the end of the String.
A null
String will return null
. An empty ("") String will
return "".
StringUtils.substring(null, *) = null StringUtils.substring("", *) = "" StringUtils.substring("abc", 0) = "abc" StringUtils.substring("abc", 2) = "c" StringUtils.substring("abc", 4) = "" StringUtils.substring("abc", -2) = "bc" StringUtils.substring("abc", -4) = "abc"
public static String substring(String s, int start, int end)
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start/end n
characters
from the end of the String.
The returned substring starts with the character in the start
position and ends before the end
position. All position counting
is zero-based -- i.e., to start at the beginning of the string use
start = 0
. Negative start and end positions can be used to
specify offsets relative to the end of the String.
If start
is not strictly to the left of end
, "" is
returned.
StringUtils.substring(null, *, *) = null StringUtils.substring("", * , *) = ""; StringUtils.substring("abc", 0, 2) = "ab" StringUtils.substring("abc", 2, 0) = "" StringUtils.substring("abc", 2, 4) = "c" StringUtils.substring("abc", 4, 6) = "" StringUtils.substring("abc", 2, 2) = "" StringUtils.substring("abc", -2, -1) = "b" StringUtils.substring("abc", -4, 2) = "ab"
public static String substringBefore(String s, String separator)
Gets the substring before the first occurrence of a separator. The separator is not returned.
A null
string input will return null
. An empty ("")
string input will return the empty string. A null
separator will
return the input string.
If nothing is found, the string input is returned.
StringUtils.substringBefore(null, *) = null StringUtils.substringBefore("", *) = "" StringUtils.substringBefore("abc", "a") = "" StringUtils.substringBefore("abcba", "b") = "a" StringUtils.substringBefore("abc", "c") = "ab" StringUtils.substringBefore("abc", "d") = "abc" StringUtils.substringBefore("abc", "") = "" StringUtils.substringBefore("abc", null) = "abc"
public static String substringAfter(String s, String separator)
Gets the substring after the first occurrence of a separator. The separator is not returned.
A null
string input will return null
. An empty ("")
string input will return the empty string. A null
separator will
return the empty string if the input string is not null
.
If nothing is found, the empty string is returned.
StringUtils.substringAfter(null, *) = null StringUtils.substringAfter("", *) = "" StringUtils.substringAfter(*, null) = "" StringUtils.substringAfter("abc", "a") = "bc" StringUtils.substringAfter("abcba", "b") = "cba" StringUtils.substringAfter("abc", "c") = "" StringUtils.substringAfter("abc", "d") = "" StringUtils.substringAfter("abc", "") = "abc"
public static String substringBeforeLast(String s, String separator)
Gets the substring before the last occurrence of a separator. The separator is not returned.
A null
string input will return null
. An empty ("")
string input will return the empty string. An empty or null
separator will return the input string.
If nothing is found, the string input is returned.
StringUtils.substringBeforeLast(null, *) = null StringUtils.substringBeforeLast("", *) = "" StringUtils.substringBeforeLast("abcba", "b") = "abc" StringUtils.substringBeforeLast("abc", "c") = "ab" StringUtils.substringBeforeLast("a", "a") = "" StringUtils.substringBeforeLast("a", "z") = "a" StringUtils.substringBeforeLast("a", null) = "a" StringUtils.substringBeforeLast("a", "") = "a"
public static String substringAfterLast(String s, String separator)
Gets the substring after the last occurrence of a separator. The separator is not returned.
A null
string input will return null
. An empty ("")
string input will return the empty string. An empty or null
separator will return the empty string if the input string is not
null
.
If nothing is found, the empty string is returned.
StringUtils.substringAfterLast(null, *) = null StringUtils.substringAfterLast("", *) = "" StringUtils.substringAfterLast(*, "") = "" StringUtils.substringAfterLast(*, null) = "" StringUtils.substringAfterLast("abc", "a") = "bc" StringUtils.substringAfterLast("abcba", "b") = "a" StringUtils.substringAfterLast("abc", "c") = "" StringUtils.substringAfterLast("a", "a") = "" StringUtils.substringAfterLast("a", "z") = ""
public static String substringBetween(String s, String tag)
Gets the String that is nested in between two instances of the same String.
A null
input String returns null
. A null
tag
returns null
.
StringUtils.substringBetween(null, *) = null StringUtils.substringBetween("", "") = "" StringUtils.substringBetween("", "tag") = null StringUtils.substringBetween("tagabctag", null) = null StringUtils.substringBetween("tagabctag", "") = "" StringUtils.substringBetween("tagabctag", "tag") = "abc"
public static String substringBetween(String s, String open, String close)
Gets the String that is nested in between two Strings. Only the first match is returned.
A null
input String returns null
. A null
open/close returns null
(no match). An empty ("") open and close
returns an empty string.
StringUtils.substringBetween("wx[b]yz", "[", "]") = "b" StringUtils.substringBetween(null, *, *) = null StringUtils.substringBetween(*, null, *) = null StringUtils.substringBetween(*, *, null) = null StringUtils.substringBetween("", "", "") = "" StringUtils.substringBetween("", "", "]") = null StringUtils.substringBetween("", "[", "]") = null StringUtils.substringBetween("yabcz", "", "") = "" StringUtils.substringBetween("yabcz", "y", "z") = "abc" StringUtils.substringBetween("yabczyabcz", "y", "z") = "abc"
public static String left(String s, int len)
Gets the leftmost len
characters of a String.
If len
characters are not available, or the String is
null
, the String will be returned without an exception. An empty
String is returned if len is negative.
StringUtils.left(null, *) = null StringUtils.left(*, -ve) = "" StringUtils.left("", *) = "" StringUtils.left("abc", 0) = "" StringUtils.left("abc", 2) = "ab" StringUtils.left("abc", 4) = "abc"
public static String right(String s, int len)
Gets the rightmost len
characters of a String.
If len
characters are not available, or the String is
null
, the String will be returned without an an exception. An
empty String is returned if len is negative.
StringUtils.right(null, *) = null StringUtils.right(*, -ve) = "" StringUtils.right("", *) = "" StringUtils.right("abc", 0) = "" StringUtils.right("abc", 2) = "bc" StringUtils.right("abc", 4) = "abc"
public static String mid(String s, int pos, int len)
Gets len
characters from the middle of a String.
If len
characters are not available, the remainder of the String
will be returned without an exception. If the String is null
,
null
will be returned. An empty String is returned if len is
negative or exceeds the length of str
.
StringUtils.mid(null, *, *) = null StringUtils.mid(*, *, -ve) = "" StringUtils.mid("", 0, *) = "" StringUtils.mid("abc", 0, 2) = "ab" StringUtils.mid("abc", 0, 4) = "abc" StringUtils.mid("abc", 2, 4) = "c" StringUtils.mid("abc", 4, 2) = "" StringUtils.mid("abc", -2, 2) = "ab"
public static String repeat(String s, 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) = ""
public static String repeat(String s, 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) = "?, ?, ?"
public 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 char
s to
be represented. If you are needing to support full I18N of your
applications consider using repeat(String, int)
instead.
repeat(String, int)
public 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 deleteWhitespace(String s)
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"
s
- the String to delete whitespace from, may be nullnull
if null String inputpublic static String removeStart(String s, 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"
public static String removeStartIgnoreCase(String s, 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"
public static String removeEnd(String s, 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"
public static String removeEndIgnoreCase(String s, 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")
public static String remove(String s, 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"
public static String remove(String s, 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"
public static String removeChars(String s, String chars)
s
- source stringchars
- string containing characters to removepublic static String removeChars(String s, char... chars)
s
- stringchars
- characters to removepublic 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"
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)
String.replaceAll(String, String)
,
Pattern.DOTALL
public 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"
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"
public 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"
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"
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"
public 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"
public static int count(String source, char c)
public static int count(String source, char c, int start)
public static boolean contains(CharSequence seq, int searchChar)
Checks if CharSequence contains a search character, handling null
.
This method uses String.indexOf(int)
if possible.
A null
or empty ("") CharSequence will return false
.
StringUtils.contains(null, *) = false StringUtils.contains("", *) = false StringUtils.contains("abc", 'a') = true StringUtils.contains("abc", 'z') = false
seq
- the CharSequence to check, may be nullsearchChar
- the character to findnull
string inputpublic static boolean contains(CharSequence seq, CharSequence searchSeq)
Checks if CharSequence contains a search CharSequence, handling null
.
This method uses String.indexOf(String)
if possible.
A null
CharSequence will return false
.
StringUtils.contains(null, *) = false StringUtils.contains(*, null) = false StringUtils.contains("", "") = true StringUtils.contains("abc", "") = true StringUtils.contains("abc", "a") = true StringUtils.contains("abc", "z") = false
seq
- the CharSequence to check, may be nullsearchSeq
- the CharSequence to find, may be nullnull
string inputpublic static boolean containsIgnoreCase(CharSequence str, CharSequence searchStr)
Checks if CharSequence contains a search CharSequence irrespective of case,
handling null
. Case-insensitivity is defined as by
String.equalsIgnoreCase(String)
.
A null
CharSequence will return false
.
StringUtils.contains(null, *) = false StringUtils.contains(*, null) = false StringUtils.contains("", "") = true StringUtils.contains("abc", "") = true StringUtils.contains("abc", "a") = true StringUtils.contains("abc", "z") = false StringUtils.contains("abc", "A") = true StringUtils.contains("abc", "Z") = false
str
- the CharSequence to check, may be nullsearchStr
- the CharSequence to find, may be nullnull
string inputpublic 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 WordUtils.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 org.apache.commons.lang3.text.WordUtils.
str
- the String to swap case, may be nullnull
if null String inputCopyright © 2020. All rights reserved.