0

String i/p - Hello\n world!! \nWelcome!!

Op -

 Hello

 World!!

 Welcome!!

If \n occurs word will be printed on the next line.

1 Answer 1

2

There is no need for a regular expression when wanting to replace one exact thing with one other exact thing, of course.

String in = "Hello\\nworld!!\\nWelcome!!";
String out = in.replace("\\n", "\n");
System.out.println(out);

> Hello
> World!!
> Welcome!!

If you want blank lines in between, replace with "\n\n" instead.

Sign up to request clarification or add additional context in comments.

2 Comments

\\n should be escaped like \\\\n
@TechBIT, no it should not. This is replace, not replaceAll.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.