Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB6,VBS
Expertise: Intermediate
Jun 10, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Replicate a string of any length

The String$ function can replicate only 1-char strings, so it seems that you need a loop to duplicate strings that contain 2 or more characters. However, this is a one-liner that does the trick:

Function ReplicateString(Source As String, Times As Long) As String
    ' build a string of spaces whose length is equal to the number of
    ' repetitions, then replace each space with the string to be repeated
    ReplicateString = Replace$(Space$(Times), " ", Source)
End Function
Note, however, that - depending on the length of the string and the number of repetitions - this approach might be slower than an optimized code based on a loop.
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date