By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

Is there a repeat characters function?

P: n/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

--
John L. Whelan
College Librarian
College of the North Atlantic
Grand Falls-Windsor, NL
Aug 17 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" <Jo*********@cna.nl.caschreef in bericht
news:81**********************************@microsof t.com...
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

Aug 17 '06 #2

P: n/a

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

Aug 17 '06 #3

P: n/a
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 L. Whelan" <Jo*********@cna.nl.cawrote in message
news:81**********************************@microsof t.com...
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

Aug 17 '06 #4

P: n/a
John L. Whelan wrote:
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
Aug 17 '06 #5

P: n/a
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
"Claes Bergefall" <lo*****@nospam.nospamwrote in message
news:uX**************@TK2MSFTNGP05.phx.gbl...
| 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 L. Whelan" <Jo*********@cna.nl.cawrote in message
| news:81**********************************@microsof t.com...
| 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
|
|
Aug 20 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.