-1

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.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Alex
  • 13
  • 1
  • What is the logic? Split at every non-word character? And what logic to get "hi" from "Hi ..."? – jogo Apr 10 '17 at 07:17
  • could you use just this code: `yourString.split(" ")`. I'm not a R developer but most languages got a function like this. – H. Pauwelyn Apr 10 '17 at 07:18

1 Answers1

1

We can use strsplit

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