用变量中的多个空格替换单个空格

时间:2019-01-17 17:58:06

标签: regex vim substitution

我有一个变量text

let text="hello world"

,并希望在两个单词之间放置多个空格。我如何以编程方式实现这一目标?这是我目前的解决方案:

let text=substitute(text," ","       ","")

但是如何在不键入每个空格的情况下放置多个空格?是否有任何函数可以放置n个空格?

1 个答案:

答案 0 :(得分:2)

您可以使用repeat()功能。来自:h repeat()

repeat({expr}, {count})                 *repeat()*
        Repeat {expr} {count} times and return the concatenated
        result.  Example: >
            :let separator = repeat('-', 80)
<       When {count} is zero or negative the result is empty.
        When {expr} is a |List| the result is {expr} concatenated
        {count} times.  Example: >
            :let longlist = repeat(['a', 'b'], 3)
<       Results in ['a', 'b', 'a', 'b', 'a', 'b'].

例如:

let text = substitute(text, " ", repeat(" ", n), "")