Method 2 : Using str_replace_all() function
This function is available in stringr package which is also used to remove the new line from a character string
Syntax: str_replace_all(string, “[\r\n]” , “”)
where
- string is the first parameter that takes string as input.
- [\r\n] is a second parameter which is a pattern to remove new lines
- “” is the third parameter that replaces when new line occurs as empty
Example: R program to remove newline from string using stringr package
R
library (stringr) # consider a string string= "Hello\nGeeks\rFor\r\nGeeks\n" # display string print ( "Original string: " ) cat (string) # string after removing new lines print ( "string after removing new lines: " ) print ( str_replace_all (string, "[\r\n]" , "" )) |
Output:
[1] "Original string: " Hello Forks Geeks [1] "string after removing new lines: " [1] "Hellow3wiki"
Remove Newline from Character String in R
In this article, we are going to see how to remove the new line from a character string in R Programming Language.
Example:
Input: String with newline: "Hello\nGeeks\rFor\r\n Geeks" Output: String after removing new line: "HelloGeeksFor Geeks"