将字符串拆分为一个或多个空格的子字符串 [英] Split string into substrings on one or more whitespaces

查看:44
本文介绍了将字符串拆分为一个或多个空格的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在出现一个或多个空格(制表符、空格...)的位置将一个字符串拆分为多个子字符串.strsplit()的文档中 它说,分割被解释为一个正则表达式.

I want to split a string into several substrings at those positions where one or more whitespaces (tab, space,...) occur. In the documentation of strsplit() it says, that split is interpreted as a regular expression.

因此我尝试了以下方法,但没有用:

Thus i tried the following, which did not work:

test = "123 nnn      dddddd"
strsplit(test, "[:space:]+")

它只返回:

[[1]]
[1] "123 nnn      dddddd"

但应该返回:

[[1]]
[1] "123" "nnn" "dddddd"

我的代码有什么问题?

推荐答案

尝试

strsplit(test, '\\s+')
[[1]]
[1] "123"    "nnn"    "dddddd"

\\s 将匹配所有空白字符.

\\s will match all the whitespace characters.

这篇关于将字符串拆分为一个或多个空格的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆