For this simple program …
import java.lang.String;
public class test {
public static void main(String[] argv) {
String s = "Hello <BSLASH>";
String sReplaced = s.replaceAll("<BSLASH>", "\\\\");
System.out.println("s = " + s);
System.out.println("sReplaced = " + sReplaced);
}
}
Why doesn’t sReplaced equal Hello \\ with 2 backslashes?
$javac test.java
$ java test
s = Hello <BSLASH>
sReplaced = Hello \
Don’t use
replaceAllfor this, usereplace:replaceAlltakes a regular expression, which is not necessary here (this is why\\\\evaluates to\).Oh, and you really don’t need
import java.lang.String– theStringclass is imported by default.