strsplit() 在字符串开头和结尾处的行为与 space 不同

strsplit() behaves different with space at the beginning and end of string

根据拆分条件 (' ') 是在字符串的开头还是结尾,它在输出列表中显示为一个项目。

#strsplit("This is a string ")

strsplit("This is a string ", ' ')
#[[1]]
#[1] "This"   "is"     "a"      "string"

#strsplit(" And this is a string", ' ')
strsplit(" And this is a string", ' ')
#[[1]]
#[1] ""       "And"    "this"   "is"     "a"      "string"

有没有办法更改此代码,以便 space 显示为两个列表的项目?

预期输出:

#strsplit("This is a string ")

strsplit("This is a string ", ' ')
#[[1]]
#[1] "This"   "is"     "a"      "string" "" 

#strsplit(" And this is a string", ' ')
strsplit(" And this is a string", ' ')
#[[1]]
#[1] ""       "And"    "this"   "is"     "a"      "string"

使用stringi::stri_split

require(stringi)
stri_split_fixed("This is a string ", ' ')
#[[1]]
#[1] "This"   "is"     "a"      "string" ""      

stri_split_fixed(" And this is a string", ' ')
#[[1]]
#[1] ""       "And"    "this"   "is"     "a"     
#[6] "string"