Navjot.jassal
Navjot.jassal

Reputation: 779

How to replace "\n " into new line?

i have one RawString the mentioned below

"Link\">https:\/\/tsad.rs\/2RBWI5E\n\n    By Tom Westbrook\n    sjda, asd 14 (sda) - sad dsad asd lower on\nTuesday, but gains in riskier currencies were capped as traders\nfretted adlaj \nladsadj \n   Available to our procedure.JPY=

only one thing need to fix in this, where i am getting "\n" with space that mentioned below.

\n   By Tom Westbrook
Text after "\n" with space want to show in Next line remove this "\n" but where i am getting only \n like below.
on\nTuesday,
i want to add null String like "". How can i achieve this. i am using like this but not getting proper result.

 sentences[i] = sentences[i].replaceAll("\\n ", "");
 sentences[i] = sentences[i].replaceAll("\\n", "");

Upvotes: 1

Views: 2872

Answers (2)

Ronak R
Ronak R

Reputation: 79

You can use below code snippet in java for desired output.

String str = s.replaceAll("\\\\n", "~")
              .replaceAll("~\\s", System.lineSeparator())
              .replaceAll("~", "");

Upvotes: 2

James Powis
James Powis

Reputation: 659

While not java here is a proof of the regex replacement (python):

>>> s = '"Link\">https:\/\/tsad.rs\/2RBWI5E\\n\\n    By Tom Westbrook\\n    sjda, asd 14 (sda) - sad dsad asd lower on\\nTuesday, but gains in riskier currencies were capped as traders\\nfretted adlaj \\nladsadj \\n   Available to our procedure.JPY='
>>> r = re.sub(r"\s*\\n+\s+", "\n", s)
>>> r = re.sub(r"\s*\\n", " ", r)                                                                                                                                               >>> print(r)
"Link">https:\/\/tsad.rs\/2RBWI5E 
By Tom Westbrook
sjda, asd 14 (sda) - sad dsad asd lower on Tuesday, but gains in riskier currencies were capped as traders fretted adlaj ladsadj
Available to our procedure.JPY=

for java it should be something like this:

sentences[i] = sentences[i].replaceAll("\s*\\n+\s+", "\n").replaceAll("\s*\\n", " ");

Upvotes: 0

Related Questions