R如何拆分字符串[重复]

时间:2022-10-06 19:19:40

This question already has an answer here:

这个问题在这里已有答案:

I have a string like

我有一个字符串

a <- "Hi. I m cool, but I need help!"

And as an output i would like to have

作为我想要的输出

"hi" "." "I" "m" "cool" "," "but" "I" "need" "help" "!"

Furthermore I don't want to use extra packages.

此外,我不想使用额外的包。

1 个解决方案

#1


1  

We can use strsplit

我们可以使用strsplit

a1 <- strsplit(a, '\\s|(?=[!,.])\\s*', perl = TRUE)[[1]]
a1[nzchar(a1)]
#[1] "Hi"   "."    "I"    "m"    "cool" ","    "but"  "I"    "need" "help" "!"   

#1


1  

We can use strsplit

我们可以使用strsplit

a1 <- strsplit(a, '\\s|(?=[!,.])\\s*', perl = TRUE)[[1]]
a1[nzchar(a1)]
#[1] "Hi"   "."    "I"    "m"    "cool" ","    "but"  "I"    "need" "help" "!"