Java replace() Method
The replace() method replaces all occurrences of the searchChar character in the string with the newChar character, and returns the new string after replacement.
Syntax
public String replace(char searchChar, char newChar)
Parameters
-
searchChar -- The original character.
-
newChar -- The new character.
Return Value
The new string generated after replacement.
Example
The following example replaces characters in the string ** **Runoob:
Example
public
class
Main
{
public
static
void
main
(
String
args
[
]
)
{
String
Str
=
new
String
(
"Runoob"
)
;
System
.
out
.
(
"Return value :"
)
;
System
.
out
.
println
(
Str.
replace
(
'o'
,
'T'
)
)
;
System
.
out
.
(
"Return value :"
)
;
System
.
out
.
println
(
Str.
replace
(
'u'
,
'D'
)
)
;
}
}
The execution result of the above program is:
返回值 :RunTTb
返回值 :RDnoob
Java String class