如何从字符串中删除所有空格?

所以" xx yy 11 22  33 "会成为"xxyy112233"。我怎样才能做到这一点?



慕姐4208626
浏览 2986回答 3
3回答

ibeautiful

一般来说,我们需要一个矢量化的解决方案,所以这里有一个更好的测试示例:whitespace <- " \t\n\r\v\f" # space, tab, newline,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # carriage return, vertical tab, form feedx <- c(&nbsp; " x y ",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# spaces before, after and in between&nbsp; " \u2190 \u2192 ", # contains unicode chars&nbsp; paste0(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # varied whitespace&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; whitespace,&nbsp;&nbsp; &nbsp; "x",&nbsp;&nbsp; &nbsp; whitespace,&nbsp;&nbsp; &nbsp; "y",&nbsp;&nbsp; &nbsp; whitespace,&nbsp;&nbsp; &nbsp; collapse = ""&nbsp; ),&nbsp; &nbsp;&nbsp; NA&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# missing)## [1] " x y "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;## [2] " ← → "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;## [3] " \t\n\r\v\fx \t\n\r\v\fy \t\n\r\v\f"## [4] NA基础R方法: gsubgsub用另一个字符串替换string(fixed = TRUE)或正则表达式(fixed = FALSE,默认值)的所有实例。要删除所有空格,请使用:gsub(" ", "", x, fixed = TRUE)## [1] "xy"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "←→"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;## [3] "\t\n\r\v\fx\t\n\r\v\fy\t\n\r\v\f" NA&nbsp;正如DWin所指出的那样,在这种情况下fixed = TRUE不是必需的,但提供稍好的性能,因为匹配固定字符串比匹配正则表达式更快。如果要删除所有类型的空格,请使用:gsub("[[:space:]]", "", x) # note the double square brackets## [1] "xy" "←→" "xy" NA&nbsp;gsub("\\s", "", x)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# same; note the double backslashlibrary(regex)gsub(space(), "", x)&nbsp; &nbsp; &nbsp; &nbsp;# same"[:space:]"是一个匹配所有空格字符的特定于R的正则表达式组。&nbsp; \s是一个独立于语言的正则表达式,它做同样的事情。该stringr方法:str_replace_all和str_trimstringr在基本R函数周围提供了更多人类可读的包装器(尽管截至2014年12月,开发版本具有构建在其上的分支stringi,如下所述)。上述命令的等价物,使用[ str_replace_all][3],是:library(stringr)str_replace_all(x, fixed(" "), "")str_replace_all(x, space(), "")stringr还有一个str_trim只删除前导和尾随空格的函数。str_trim(x)&nbsp;## [1] "x y"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "← →"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "x \t\n\r\v\fy" NA&nbsp; &nbsp;&nbsp;str_trim(x, "left")&nbsp; &nbsp;&nbsp;## [1] "x y "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"← → "&nbsp; &nbsp;&nbsp;## [3] "x \t\n\r\v\fy \t\n\r\v\f" NA&nbsp; &nbsp; &nbsp;str_trim(x, "right")&nbsp; &nbsp;&nbsp;## [1] " x y"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" ← →"&nbsp; &nbsp;&nbsp;## [3] " \t\n\r\v\fx \t\n\r\v\fy" NA&nbsp; &nbsp; &nbsp;&nbsp;该stringi方法:stri_replace_all_charclass和stri_trimstringi基于独立于平台的ICU库构建,并具有广泛的字符串操作功能。以上的等价物是:library(stringi)stri_replace_all_fixed(x, " ", "")stri_replace_all_charclass(x, "\\p{WHITE_SPACE}", "")这里"\\p{WHITE_SPACE}"是对于该组被认为是空白,相当于Unicode码位的替换的语法"[[:space:]]","\\s"和space()。对于更复杂的正则表达式替换,也有stri_replace_all_regex。stringi还有修剪功能。stri_trim(x)stri_trim_both(x)&nbsp; &nbsp; # samestri_trim(x, "left")stri_trim_left(x)&nbsp; &nbsp; # samestri_trim(x, "right")&nbsp;&nbsp;stri_trim_right(x)&nbsp; &nbsp;# same
打开App,查看更多内容
随时随地看视频慕课网APP