Pash0002
Pash0002

Reputation: 110

Convert '\n' character from string to newline in java using regex

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

Op -

 Hello

 World!!

 Welcome!!

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

Upvotes: 0

Views: 367

Answers (1)

rzwitserloot
rzwitserloot

Reputation: 102795

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.

Upvotes: 2

Related Questions