Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
strsplit()的行为与字符串开头和结尾的空格不同_R_Strsplit - Fatal编程技术网

strsplit()的行为与字符串开头和结尾的空格不同

strsplit()的行为与字符串开头和结尾的空格不同,r,strsplit,R,Strsplit,根据拆分条件(“”)是否位于字符串的开头或结尾,它将显示为输出列表中的一项 #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] ""

根据拆分条件(
“”
)是否位于字符串的开头或结尾,它将显示为输出列表中的一项

#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"
是否有办法更改此代码,使空格显示为两个列表的一项

预期产出:

#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"

请注意,这是在“详细信息”部分的
?strsplit
库(ore)中记录的行为;ore.split(“,“这是一个字符串”)