I want to replace newline with "\n" but my code isn't working.
str.replaceAll("\n","\n"));
Expected: "Hello\n World"
Actual:
"Hello
World"
If you want the backslash to show up you have to escape it. Otherwise it will be interpreted as a new line.
str = str.replaceAll("\n","\\\\n"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With