Is there a repeat characters function?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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,

Even if it did exist I would do it in this way

dim John as String
For i as integer = 0 to 4
John += "John"
Next

(And if it exist you can be sure that it is done internal the same)

I would not take the time to search

Cor
 
John L. Whelan napisal(a):
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

Hi,

I don't know such a function but I wrote for you one:

Private Function Repeat(ByVal StringToRepeat As String, ByVal
HowManyTimes As Integer)
Dim Result As String = ""
For i As Integer = 1 To HowManyTimes
Result += StringToRepeat
Next
Return Result
End Function

Hope this helps,
sweet_dreams
 
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
 
John said:
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"?

I can do this!

Dim s As String
s = StrDup(5, "x")
s = s.Replace("x", "John")

Or for a longer string:-

Dim b As New StringBuilder
b.Append("x", 500)
b.Replace("x", "John")

Andrew
 
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
|
|
 
Back
Top