java字符串函数用法汇总

替换字符串中的字符
例如有如下x的字符串

String x = "[kllkklkkkkllkk]";
要将里面的“kk”替换为++,可以使用两种方法得到相同的结果

replace(CharSequence target, CharSequence replacement) —— x.replace("kk", "++")

replaceAll(String regex, String replacement) —— x.replaceAll("kk", "++")

String str = "test.doc";
String newStr = str.replaceAll("doc","html");

String strSrc = "abckjhlshjsjh中国人fiahh中国人h rewrew";
String strDst = "中国";
String tar ="美国";
String strEnd = strSrc.replaceAll(strDst, tar);
System.out.println(strEnd);

原文地址:https://www.cnblogs.com/Andrew520/p/8196324.html