potato-pyy 2012-09-04 11:31
浏览 195
已采纳

java 替换 \ 的出错?????

String title = "HD_爸爸,\玛丽和我".replaceAll("\", "");
我要替换这里面去掉这里面的\
报错:Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^ 怎么解决?

  • 写回答

2条回答 默认 最新

  • _1_1_7_ 2012-09-04 14:40
    关注

    [code="java"] static final Pattern pattern=Pattern.compile("\\");
    public static void main(String[] args) throws Exception {
    String s = "HD_爸爸,\玛丽和我";
    String s1 = s.replaceAll("\\", "");
    String s2 = s.replace('\', ' ');
    System.out.println(s1);
    System.out.println(s2);

        //多次调用时,用以下方式性能更好
        Matcher m=pattern.matcher(s);
        String s3=m.replaceAll("");
        System.out.println(s3);
    }[/code]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?