Remove end-of-line characters from a comment body

John Colosi April 7, 2023

I want to remove end-of-line characters from a comment body. Things I tried:

  • comment.body.trim
    • trim doesn't remove end-of-line characters for whatever reason.
  • xmlEncode and other encodings
    • did not seem to encode the end-of-line characters so I could remove them
  • replaceAll(regex,string)
    • the regex is applied line-by-line, so trailing end-of-line characters get processed, but not removed

 

Ultimately I decided to use indexOf to get the first instance of an "\r" character. But in order to effectively remove them, I had to make sure at least one was there. So I ended with this solution.

  • comment.body.left(comment.body.concat("\r").indexOf("\r"))

This is obviously ugly. There should be a better way to remove end-of-line characters. Please help.

 

1 answer

1 vote
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 7, 2023

Hi @John Colosi 

Have you tried to use split() on the newlines (effectively removing them) and then join() with the character of your choice to concatenate the text?

Depending on the source text, you may want to check for both newline and line feeds.

Kind regards,
Bill

Suggest an answer

Log in or Sign up to answer