一、替换
replaceAll(String oldReg,String newReg)方法是用于替换的方法 ,oldReg是用正则表达式表示要被替换的字符串,newReg是用正则表达式表示替换的老字符串的新字符串。
1、在正则表达式中 字符串"\"要用“\\\\”表示,字符串"\\"要用“\\\\\\\\”表示.
public class TestString {
public static void main(String[] args){
TestString test = new TestString();
test.testReplace();
}
/**
* 测试替换
*
*/
public void testReplace(){
String strRet = "\t\tddd";
String strTest = "\\k\\d\\l";
strRet.replaceAll("\\t","k");
/**
// "\t"表示tab键值,而这tab键值是表示四个空格
//所以"\t\tddd"表示八个空格加三个ddd的字符串
//所以strRet的值为" ddd"
* strRet.replaceAll("\\t","k")就没有替换成功
*
*/
System.out.println("hjhDebug-----------::: " + strRet);
//在正则表达式中 字符串"\"要用“\\\\”表示,
//字符串"\\"要用“\\\\\\\\”表示
// System.out.println("hjhDebug-------22 is::" +strTest.replaceAll("\\\\","\\\\\\\\"));
if (strTest.indexOf("\\")!=-1) {
System.out.println("hjhDebug---" +strTest + " ---- is::" +strTest.replaceAll("\\\\",""));
}
strTest = "I/odewwe/dwwew/e/f/we";
if (strTest.indexOf("/")!=-1) {
System.out.println("hjhDebug-------I/o is::" +strTest.replaceAll("/",""));
}
}
}