是否可以在这样的字符串中插入换行符,以便自动调整以便不分割单词?
nif <- as.character(c("I am a string","So am I","I am also a string but way to long"))
我在帖子中找到了这个code,但是它会分开单词并在每个字符串之后添加一个换行符,我想避免
gsub('(.{1,20})','\\1\n',nif)
我想要的输出是这样的:
"I am a string" "So am I" "I am also a string but \n way to long"
解决方法
你也可以使用strwrap.
strwrap(nif,20) # [1] "I am a string" "So am I" "I am also a string" # [4] "but way to long" sapply( strwrap(nif,20,simplify=FALSE),paste,collapse="\n" ) # [1] "I am a string" "So am I" # [3] "I am also a string\nbut way to long"