String temp = "aa|bb|cc;dd";
System.out.println(("|").equals("|"));//true
System.out.println(("|").equals("\\|"));//false
System.out.println(("\\|").equals("\\|"));//true
System.out.println(temp.contains("|"));//true
System.out.println(temp.contains("\\|"));//false
System.out.println(temp.replaceAll("|", " "));// a a | b b | c c ; d d ,按字切分
System.out.println(temp.replaceAll("\\|", " "));//有效
System.out.println(temp.replaceAll(";", " "));//有效
System.out.println(temp.replace("|", " "));//有效
System.out.println(temp.replace("\\|", " "));
System.out.println(temp.replace(";", " "));//有效
String [] a1 = temp.split("|");//按字切分
String [] a2 = temp.split("\\|");//有效
String [] a3 = temp.split(";");
由上述结果可知,|字符需要特殊处理,其他字符不需要
本文深入探讨了Java中字符串处理的细节,包括特殊字符处理、字符串比较、替换和分割等操作。通过实例演示了如何正确使用正斜杠进行转义,以及如何避免常见的字符串操作误区。
913

被折叠的 条评论
为什么被折叠?



