使用R从字符串中删除所有换行符(输入符号)
问题描述:
如何使用R删除字符串中的所有换行符(输入符号)?
How to remove all line breaks (enter symbols) from the string using R?
我已经尝试过gsub("\n", "", my_string)
,但是它不起作用,因为新行和换行符不相等.
I've tried gsub("\n", "", my_string)
, but it doesn't work, because new line and line break aren't equal.
谢谢!
答
您需要剥离\r
和\n
才能删除回车符和换行符.
You need to strip \r
and \n
to remove carriage returns and new lines.
x <- "foo\nbar\rbaz\r\nquux"
gsub("[\r\n]", "", x)
## [1] "foobarbazquux"
或
library(stringr)
str_replace_all(x, "[\r\n]" , "")
## [1] "foobarbazquux"