I am being given a string from an API that contains \n
in it.
For example:
"Hello there.\n How are you?\n"
I need to remove the \n
(as there currently have no effect in the string) and replace them with the intended line break.
This will produce:
"Hello there.
How
…
Top comments (1)
It's unclear what you're asking because:
\n
in a double-quoted string is already a line break, and for it to appear as in the question, it's probably escaped as\\n
or something, but we can't tell from the way the question is phrased/n
(with the forward slash. You start by saying you'd like line breaks, and then end up by giving an example with/n
in it, which doesn't have any special meaning.\r\n
instead. I can't remember the ins and outs of this right now though.To directly answer, using
String.replace
is probably what you want:Given we're not sure what you're going for though, you could try out a bunch of variations like these:
Until you get whatever you're after
If you edit the question on SO, even though it's closed, it'll get put in the queue to get reviewed for re-opening.