Java replaceAll() Method
The replaceAll() method uses the given replacement parameter to replace all substrings of the string that match the given regular expression.
Syntax
public String replaceAll(String regex, String replacement)
Parameters
-
regex -- The regular expression that matches this string.
-
replacement -- The string used to replace each match.
Return Value
Returns the replaced string on success, or the original string on failure.
Example
Example
public
class
Test
{
public
static
void
main
(
String
args
[
]
)
{
String
Str
=
new
String
(
"www.google.com"
)
;
System
.
out
.
(
"Return value on successful match :"
)
;
System
.
out
.
println
(
Str.
replaceAll
(
"(.)google(.)"
,
"runoob"
)
)
;
System
.
out
.
(
"Return value on failed match :"
)
;
System
.
out
.
println
(
Str.
replaceAll
(
"(.)taobao(.)"
,
"runoob"
)
)
;
}
}
The execution result of the above program is:
匹配成功返回值 :runoob
匹配失败返回值 :www.google.com
Java String class