Trim the extra \ n (new string characters) in a string in R

I would like to disable unnecessary additional new string characters in strings using R. For example, if I have a string:

"This is an example test string. \n \n \n"

I would like it to look like this:

"This is an example test string. \n"
+4
source share
1 answer

Try

 x <- gsub("\\n\\s*", "\n", x)

This searches for any new line followed by a space, and replaces it with one new line.

+3
source

All Articles