VBScript 中是否有任何内置函数可以将字符串重复 N 次?

Is there any build-in function in VBScript to repeat a string N-times?

VBScript 中有一个函数 String(number,character) returns 一个包含指定长度的重复字符的字符串。例如:

String(5, "A")     ' output: "AAAAA"

有没有重复字符串的功能?例如:

RepeatString(5, "Ab")     ' output "AbAbAbAbAb"

不,没有内置任何内容。而是:

n      = 5
str    = "Ab"
result = replace(space(n), " ", str)

对于一般的简单解决方案

Function RepeatString( number, text )
    Redim buffer(number)
    RepeatString = Join( buffer, text )
End Function

但如果文本很短但重复次数很多,这是一个更快的解决方案

Function RepeatString( ByVal number, ByVal text )
    RepeatString=""
    While (number > 0)
        If number And 1 Then 
            RepeatString = RepeatString & text
        End If 
        number = number \ 2 
        If number > 0 Then
            text = text & text 
        End If 
    Wend
End Function

为了代码简洁,接受的答案很好。但是下面的函数实际上快了 10 倍。而且速度是 RepeatString() 的两倍。它使用 Mid 的一个鲜为人知的特性,它一次性用 repeating 模式填充字符串缓冲区的剩余部分...

Function Repeat$(ByVal n&, s$)
    Dim r&
    r = Len(s)
    If n < 1 Then Exit Function
    If r = 0 Then Exit Function
    If r = 1 Then Repeat = String$(n, s): Exit Function
    Repeat = Space$(n * r)
    Mid$(Repeat, 1) = s: If n > 1 Then Mid$(Repeat, r + 1) = Repeat
End Function