Java中replace和replaceall的区别
1>replace的参数是char和CharSequence,既可以支持字符替换,也可以支持字符串替换。
2>replaceall参数是regex, replacement,regex表示是正则表达式。
String str = "wel2come3Souhe";
String str1 = str.replace("e", "E");
String str3 = str.replace('e', 'E');
System.out.println("replace字符串==" + str1);
System.out.println("replace字符=="+str3);
String str2 = str.replaceAll("\\d", "a");//将数字替换a
System.out.println("replaceAll==" + str2);
String str1 = str.replace("e", "E");
String str3 = str.replace('e', 'E');
System.out.println("replace字符串==" + str1);
System.out.println("replace字符=="+str3);
String str2 = str.replaceAll("\\d", "a");//将数字替换a
System.out.println("replaceAll==" + str2);
输出结果:
replace字符串==wEl2comE3SouhE
replace字符==wEl2comE3SouhE
replaceAll==welacomeaSouhe
replace字符==wEl2comE3SouhE
replaceAll==welacomeaSouhe
比较结果:replace替换字符和字符串都一样,replaceall是根据正则表达式来进行替换的