Claes (and Andrew),
I would recommend setting the capacity of the StringBuilder when you create
it, this will prevent any intermediate reallocations of its internal buffer.
Especially in this case where you have a clear indication of how large the
final string will be.
| Dim sb As New System.Text.StringBuilder(count * value.Length)
The "count * value.Length" causes the StringBuilder to allocate a "count *
value.Length" character buffer.
Remember that New StringBuilder() allocates a 16 character buffer, which it
then doubles each time it (the buffer) needs to be expanded.
If you want "John" duplicated 500 times, StringBuilder will need to
reallocate its buffer a number of a significant # of times. These
reallocations can cause a performance problem in that the buffer itself
needs to be copied each time it is reallocated, plus each old buffer will
add pressure to the GC...
In cases where I don't have a clear indication of how large the final string
will be I try to use a guesstimate. For example the average/typical length
of the final string.
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
| Don't think there is a built in way, but it's not so hard to build a
| function that does it. Try this:
|
| Public Function StrDup(ByVal count As Integer, ByVal value As String) As
| String
| Dim sb As New System.Text.StringBuilder
| While count > 0
| sb.Append(value)
| count -= 1
| End While
| Return sb.ToString()
| End Function
|
| /claes
|
| | > Hi Everyone:
| >
| > I know that I can repeat a single character using the StrDup function.
Is
| > there a function that will allow me to repeat a string? StrDup(5,"John")
| > returns "JJJJJ." Is there a function (or workaround) that will
| > return"JohnJohnJohnJohnJohn"?
| >
| > Thanks in advance.
| >
| > John
| >
| > --
| > John L. Whelan
| > College Librarian
| > College of the North Atlantic
| > Grand Falls-Windsor, NL
|
|