我已经编写了执行此操作的代码:
text <- "abcdef"
substring(text, first = (1:(nchar(text)/2)) * 2 - 1, last = (1:(nchar(text)/2)) * 2)
结果:
"ab" "cd" "ef"
它适用于小型字符串,但不适用于长字符串。
任何建议,我怎样才能更快地做到这一点?
尝试trimws(gsub("(.{2})", "\\1 ", text))
和stringi::stri_sub(text, seq(1, stringi::stri_length(text), by = 2), length = 2
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句