请说说String的replace和replaceAll方法的区别?
replace方法:支持字符和字符串的替换。
public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)
replaceAll方法:基于正则表达式的字符串替换。
public String replaceAll(String regex, String replacement)
测试代码:
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.
打印结果:
Hello c++ Java is a language.
Hello c++ c++is a language.