Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

replace `_' (underscore) in a string with "\_" (backslash and underscore)



I want to replace the `_' (underscore) character in a string with "\_" (backslash and underscore). The following is the test code:

public class Test {
public static void main(String[] args) {
String source = "ABC_DEF_GH\\I\\_JK";
System.out.println(source);

String replaced = source.replaceAll("_", "+");
System.out.println(replaced);

replaced = source.replaceAll("_", "\\_");
System.out.println(replaced);
}
}

This is the output:

ABC_DEF_GH\I\_JK
ABC+DEF+GH\I\+JK
ABC_DEF_GH\I\_JK

I actually expected the third line to be:
ABC\_DEF\_GH\I\\_JK

But to get the result I want, I have to use
source.replaceAll("_", "\\\\_");

Does anybody know why it works this way?