comma in regex in String.replaceAll() method?

Posted by kknight on Stack Overflow See other posts from Stack Overflow or by kknight
Published on 2010-06-17T18:34:52Z Indexed on 2010/06/17 18:43 UTC
Read the original article Hit count: 175

Filed under:

My code tries to replace "," with "/" in a string. Should I escape "," in the regex string? Both of the two code snippets generated the same results, so I am confused.

Code snippet 1:

    String test = "a,bc,def";

    System.out.println(test.replaceAll("\\,", "/"));

Code snippet 2:

    String test = "a,bc,def";

    System.out.println(test.replaceAll(",", "/"));

Should I use "," or "\,"? Which is safer?

Thanks.

© Stack Overflow or respective owner

Related posts about java