Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java replace() Method

Java replace() Method

Java String ClassJava String class


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

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

.

print

(

"Return value :"

)

;

       

System

.

out

.

println

(

Str.

replace

(

'o'

,

'T'

)

)

;

       

System

.

out

.

print

(

"Return value :"

)

;

       

System

.

out

.

println

(

Str.

replace

(

'u'

,

'D'

)

)

;

   

}

}

The execution result of the above program is:


返回值 :RunTTb
返回值 :RDnoob

Java String ClassJava String class