如何从字符串中删除所有空格?
因此“xx yy 11 22 33”
将变成“xxyy112233”
。我怎样才能实现这个目标?
So " xx yy 11 22 33 "
will become "xxyy112233"
. How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
一般来说,我们想要一个矢量化的解决方案,所以这里有一个更好的测试示例:
基本 R 方法:
gsub
gsub
替换字符串 (fixed = TRUE
) 或正则表达式 (fixed = FALSE
,默认值)用另一根绳子。要删除所有空格,请使用:正如 DWin 所指出的,在这种情况下,
fixed = TRUE
不是必需的,但可以提供稍好的性能,因为匹配固定字符串比匹配正则表达式更快。如果要删除所有类型的空格,请使用:
"[:space :]"
是匹配所有空格字符的特定于 R 的正则表达式组。\s
是一个独立于语言的正则表达式,具有相同的功能。stringr
方法:str_replace_all
和str_trim
stringr
围绕基本 R 函数提供了更多人类可读的包装器(尽管从 2014 年 12 月开始,开发版本有一个基于stringi
构建的分支,如下所述)。使用 [str_replace_all][3]
的上述命令的等效项是:stringr
也有一个str_trim
函数,仅删除前导和尾随空格。stringi
方法:stri_replace_all_charclass
和stri_trim
stringi
构建在独立于平台的 ICU 库,并具有一组广泛的字符串操作函数。上述的等价是:这里
"\\p{WHITE_SPACE}"
是替代方案集合的语法被视为空白的 Unicode 代码点,相当于"[[:space:]]"
、"\\s"
和space()
。对于更复杂的正则表达式替换,还有stri_replace_all_regex
。stringi
还具有修剪函数。In general, we want a solution that is vectorised, so here's a better test example:
The base R approach:
gsub
gsub
replaces all instances of a string (fixed = TRUE
) or regular expression (fixed = FALSE
, the default) with another string. To remove all spaces, use:As DWin noted, in this case
fixed = TRUE
isn't necessary but provides slightly better performance since matching a fixed string is faster than matching a regular expression.If you want to remove all types of whitespace, use:
"[:space:]"
is an R-specific regular expression group matching all space characters.\s
is a language-independent regular-expression that does the same thing.The
stringr
approach:str_replace_all
andstr_trim
stringr
provides more human-readable wrappers around the base R functions (though as of Dec 2014, the development version has a branch built on top ofstringi
, mentioned below). The equivalents of the above commands, using [str_replace_all][3]
, are:stringr
also has astr_trim
function which removes only leading and trailing whitespace.The
stringi
approach:stri_replace_all_charclass
andstri_trim
stringi
is built upon the platform-independent ICU library, and has an extensive set of string manipulation functions. The equivalents of the above are:Here
"\\p{WHITE_SPACE}"
is an alternate syntax for the set of Unicode code points considered to be whitespace, equivalent to"[[:space:]]"
,"\\s"
andspace()
. For more complex regular expression replacements, there is alsostri_replace_all_regex
.stringi
also has trim functions.我刚刚了解了“stringr”包,它可以使用 str_trim( , side="both") 从字符串的开头和结尾删除空格,但它还有一个替换功能,以便:
I just learned about the "stringr" package to remove white space from the beginning and end of a string with str_trim( , side="both") but it also has a replacement function so that:
使用
[[:blank:]]
匹配任何类型的水平空白字符。Use
[[:blank:]]
to match any kind of horizontal white_space characters.请注意,上面写的灵魂仅删除空格。如果您还想删除制表符或换行符,请使用
stringi
包中的stri_replace_all_charclass
。Please note that soultions written above removes only space. If you want also to remove tab or new line use
stri_replace_all_charclass
fromstringi
package.tidyverse 包
stringr
中的函数str_squish()
发挥了神奇作用!The function
str_squish()
from packagestringr
of tidyverse does the magic!可以考虑另一种方法
\\s:匹配空格、制表符、垂直制表符、换行符、换页符、回车符
*:匹配至少 0 次
Another approach can be taken into account
\\s: Matches Space, tab, vertical tab, newline, form feed, carriage return
*: Matches at least 0 times
要删除
.00
之后的空格,请使用trimws()
函数。To remove space after
.00
use thetrimws()
function.从 stringr 库中,您可以尝试以下操作:
删除填充空白
库(字符串)
<前><代码> 2. 1.
| |
维维
str_replace_all(str_trim("xx yy 11 22 33"),"","")
From stringr library you could try this:
Remove fill blank
library(stringr)