例如,我有一个字符串
x <- 'what are you talking about'
如何将其拆分为两个字符串'what'
和'are you talking about'
?
答案 0 :(得分:1)
匹配而不是拆分。
> x <- 'what are you talking about'
> library(stringi)
> stri_extract_all(x, regex="^\\S+|\\S.*")[[1]]
[1] "what"
[2] "are you talking about"
或强>
> library(stringr)
> str_split(x, perl("^\\S+\\K\\s+"))
[[1]]
[1] "what"
[2] "are you talking about"
答案 1 :(得分:1)
你可以做到这一点,因为strsplit
在这里不会很好看:
val <- regexpr("\\s",x)
substring(x, c(1,val+1), c(val-1,nchar(x)) )
#[1] "what" "are you talking about"