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(“,“这是一个字符串”)
。