public class StringUtils
extends java.lang.Object
Constructor and Description |
---|
StringUtils() |
Modifier and Type | Method and Description |
---|---|
static boolean |
isEmpty(java.lang.String str)
Checks if a String is empty ("") or null.
|
static java.lang.String |
replace(java.lang.String text,
java.lang.String repl,
java.lang.String with)
Replaces all occurrences of a String within another String.
|
static java.lang.String |
replace(java.lang.String text,
java.lang.String repl,
java.lang.String with,
int max)
Replaces a String with another String inside a larger String,
for the first
max values of the search String. |
static java.lang.String |
replaceChars(java.lang.String str,
char searchChar,
char replaceChar)
Replaces all occurrences of a character in a String with another.
|
static java.lang.String |
replaceChars(java.lang.String str,
java.lang.String searchChars,
java.lang.String replaceChars)
Replaces multiple characters in a String in one go.
|
static java.lang.String |
replaceOnce(java.lang.String text,
java.lang.String repl,
java.lang.String with)
Replaces a String with another String inside a larger String, once.
|
static java.lang.StringBuffer |
stringSubstitution(java.lang.String argStr,
java.util.Map vars,
boolean isLenient)
Perform a series of substitutions.
|
public static java.lang.String replaceOnce(java.lang.String text, java.lang.String repl, java.lang.String with)
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("aba", null, null) = "aba" StringUtils.replaceOnce("aba", null, null) = "aba" StringUtils.replaceOnce("aba", "a", null) = "aba" StringUtils.replaceOnce("aba", "a", "") = "aba" StringUtils.replaceOnce("aba", "a", "z") = "zba"
text
- text to search and replace in, may be nullrepl
- the String to search for, may be nullwith
- the String to replace with, may be nullnull
if null String inputreplace(String text, String repl, String with, int max)
public static java.lang.String replace(java.lang.String text, java.lang.String repl, java.lang.String with)
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("aba", null, null) = "aba" StringUtils.replace("aba", null, null) = "aba" StringUtils.replace("aba", "a", null) = "aba" StringUtils.replace("aba", "a", "") = "aba" StringUtils.replace("aba", "a", "z") = "zbz"
text
- text to search and replace in, may be nullrepl
- the String to search for, may be nullwith
- the String to replace with, may be nullnull
if null String inputreplace(String text, String repl, String with, int max)
public static java.lang.String replace(java.lang.String text, java.lang.String repl, java.lang.String with, 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("abaa", null, null, 1) = "abaa" StringUtils.replace("abaa", null, null, 1) = "abaa" StringUtils.replace("abaa", "a", null, 1) = "abaa" StringUtils.replace("abaa", "a", "", 1) = "abaa" 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 nullrepl
- the String to search for, may be nullwith
- the String to replace with, may be nullmax
- maximum number of values to replace, or -1
if no maximumnull
if null String inputpublic static java.lang.String replaceChars(java.lang.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 java.lang.String replaceChars(java.lang.String str, java.lang.String searchChars, java.lang.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 boolean isEmpty(java.lang.String str)
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
NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank().
str
- the String to check, may be nulltrue
if the String is empty or nullpublic static java.lang.StringBuffer stringSubstitution(java.lang.String argStr, java.util.Map vars, boolean isLenient)
argStr
- target stringvars
- name/value pairs used for substitutionisLenient
- ignore failuresCopyright © 2000-2013 Apache Software Foundation. All Rights Reserved.