将变量中的单个 space 替换为多个 space

Substitute single space with multiple spaces in variable

我有一个变量text:

let text="hello world"

并且想在两个词之间放置多个空格。我怎样才能以编程方式实现这一目标?这是我目前的解决方案:

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

但是我怎么能在不输入每个空格的情况下放置多个空格呢?是否有任何功能可以放置 n 个空格?

您可以使用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), "")