【发布时间】:2021-12-12 12:43:40
【问题描述】:
我有一个字符向量。
x <- c('This is a simple text', 'this is a veryyyyyyyyyy long word', 'Replacethis andalsothis')
我想在长于n 个字符的单词之间插入一个空格。对于这个例子,我们可以考虑n = 10。我更喜欢regex 解决方案,但如果您认为还有其他选择,我不介意尝试。
我正在寻找的输出 -
c('This is a simple text', 'this is a veryyyyyyy yyy long word', 'Replacethi s andalsothi s')
我尝试使用the solution from this post 对我的数据进行必要的更改,但它没有提供所需的输出。
sub('(.{10})(?=\\S)//g', '\\1 ', x, perl = TRUE)
#[1] "This is a simple text" "this is a veryyyyyyyy long word" "Replacethis andalsothis"
【问题讨论】: