builder-7000
builder-7000

Reputation: 7627

Substitute single space with multiple spaces in variable

I have a variable text:

let text="hello world"

and would like to put multiple spaces between the two words. How could I achieve this programmatically? This is my current solution:

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

but how could I put multiple spaces without typing each of them? Is there any function to put n number of spaces?

Upvotes: 0

Views: 46

Answers (1)

DJMcMayhem
DJMcMayhem

Reputation: 7669

You can use the repeat() function. From :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'].

For example:

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

Upvotes: 2

Related Questions