Say I have a string like this:
String message = "Hi \n Prakash";
I want to print messsage exactly the way it is rather than printing a newline for \n.
So the desired output is:
Hi \n Prakash
and not
Hi
Prakash
Please note that we are not allowed to modify the string message. How do I do this?
Edit: I've used \n just as an example. There could be similar characters like \t which could possibly be in my string.
This can be done with the help of StringEscapeUtils class from Apache Commons Text.
import org.apache.commons.lang3.StringEscapeUtils;
String message = "Hi \n Prakash";
System.out.println(StringEscapeUtils.escapeJava(message));
The output is:
Hi \n Prakash
Thanks Dima for providing the link in answer.
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