|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Is there a repeat characters function?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 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 Show quoteHide quote "John L. Whelan" <John.Whe***@cna.nl.ca> schreef in bericht news:81E90071-1AC1-4A61-B67F-C4DB1D46F83C@microsoft.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 John L. Whelan napisal(a):
> Hi Everyone: Hi,> > 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 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 Show quoteHide quote "John L. Whelan" <John.Whe***@cna.nl.ca> wrote in message news:81E90071-1AC1-4A61-B67F-C4DB1D46F83C@microsoft.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 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. -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Claes Bergefall" <louplou@nospam.nospam> wrote in message news:uX1Jy4gwGHA.4972@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" <John.Whe***@cna.nl.ca> wrote in message | news:81E90071-1AC1-4A61-B67F-C4DB1D46F83C@microsoft.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 | | John L. Whelan wrote:
> Hi Everyone: I can do this!> > 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"? 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 |
|||||||||||||||||||||||