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'
?
Open TextPad and the file you want to edit. Click Search and then Replace. In the Replace window, in the Find what section, type ^\n (caret, backslash 'n') and leave the Replace with section blank, unless you want to replace a blank line with other text. Check the Regular Expression box.
Use the String. replace() method to remove all line breaks from a string, e.g. str. replace(/[\r\n]/gm, ''); . The replace() method will remove all line breaks from the string by replacing them with an empty string.
You could use:
myString = myString.replaceAll("[\r\n]+", "\n");
In Android I managed to remove more than 2 line breaks with this
textPiece = textPiece.replaceAll("\n\n\n+", "\n\n");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With