Home > Blockchain >  How can I split a string "a\n\nb" into c("a", "", "", &quo
How can I split a string "a\n\nb" into c("a", "", "", &quo

Time:01-07

When I stringr::str_split "\n\na\n" by "\n", I obtain c("", "", "a", "").

I expected I can obtain c("a", "", "", "b") when I stringr::str_split "a\n\nb" by "\n", but I obtained c("a", "", "b") instead. How can I obtain c("a", "", "", "b") by splitting "a\n\n\a"?

Try:

stringr::str_split("a\n\nb", "\n")

Expect:

c("a", "", "", "b")

Result:

c("a", "", "b")

CodePudding user response:

Assuming that we want to extract each character separately, just split on the blank and remove all the \n to return blanks for the split elements

str_remove_all(strsplit(str1, "")[[1]], "\n")
[1] "a" ""  ""  "b"

Or use perl = TRUE with \n?

strsplit(str1, "\n?", perl = TRUE)[[1]]
[1] "a" ""  ""  "b"

If it should be at each \n only

strsplit(str_replace(str1, "(\n\\S)", "\n\\1"), "\n")[[1]]
[1] "a" ""  ""  "b"

data

str1 <- "a\n\nb"

CodePudding user response:

You are getting the correct result, even if it's not what you expect. The length of the result equals the number of delimiters in the string 1. Thus if you want an extra field, you need to add an extra delimiter:

x1 <- "a\n\nb"
x2 <- "abc\n\ndef"

strsplit(gsub("(\n{2,})", "\\1\n", x1), "\n")[[1]]
[1] "a" ""  ""  "b"

strsplit(gsub("(\n{2,})", "\\1\n", x2), "\n")[[1]]
[1] "abc" ""    ""    "def"
  • Related