I do understand the concept, where we need 2 backslashes when dealing with regex - https://stackoverflow.com/a/1701876/72437
The following code able to split hello and world without issue.
String message = "hello\nworld";
String[] result = message.split("\\n");
// hello
// world
for (String r : result) {
System.out.println(r);
}
However, if I use 1 backslash, it works too. (Able to split hello and world too)
String message = "hello\nworld";
String[] result = message.split("\n");
// hello
// world
for (String r : result) {
System.out.println(r);
}
I expect using only 1 backslash for regex will not work in Java. But, it works. May I know why is it so?