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

Java replaceAll() Method

Java replaceAll() Method

Java String ClassJava String class


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

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

.

print

(

"Return value on successful match :"

)

;

       

System

.

out

.

println

(

Str.

replaceAll

(

"(.)google(.)"

,

"runoob"

)

)

;

       

System

.

out

.

print

(

"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 ClassJava String class