madprops
madprops

Reputation: 4023

Remove multiple linebreaks

How can I remove multiple linebreaks from a string so I only get one linebreak if any. For example I have a string with "\r\n\r\n\r\n\r\n\r\n\r\n\r\n", how can I turn that into a single '\n' ?

Upvotes: 7

Views: 8220

Answers (2)

Rafael
Rafael

Reputation: 6369

In Android I managed to remove more than 2 line breaks with this

textPiece = textPiece.replaceAll("\n\n\n+", "\n\n");

Upvotes: 2

Reimeus
Reimeus

Reputation: 159844

You could use:

myString = myString.replaceAll("[\r\n]+", "\n");

Upvotes: 30

Related Questions