String difference between object and replace replaceAll of?

replace method: support the replacement characters and strings.

public String replace(char oldChar, char newChar)
 
public String replace(CharSequence target, CharSequence replacement)

 

 

replaceAll Method: replacement string based on regular expression.

public String replaceAll(String regex, String replacement)

 

 

Test code:

String str = "Hello Java. Java is a language.";
System.out.println(str.replace("Java.", "c++"));//打印 Hello c++ Java is a language.
System.out.println(str.replaceAll("Java.", "c++"));//打印 Hello c++ c++is a language.

 

Print Results:

Hello c++ Java is a language.
Hello c++ c++is a language.

 

ps: once thought replace () is the first match of the character or string replacement, replaceAll () is a character string or string replacement target all match, from the results of this perception is wrong.

 

 


 

All resources resources are summarized in the public No.

Guess you like

Origin www.cnblogs.com/ConstXiong/p/11808152.html