jjjamie Posted April 1, 2003 Posted April 1, 2003 Somewhere in the framework there is a function to repeat a given character a specified amount of times and return a string but I can't seem to find it. Does anyone know what this function is called? It's really frustraiting because I know it exists! Quote
*Gurus* divil Posted April 1, 2003 *Gurus* Posted April 1, 2003 One of the String constructors accepts a character and an integer number of times to repeat it. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
michael_hk Posted February 14, 2005 Posted February 14, 2005 How about repeating a string instead of a char. Something very simple but I can't find it. Thanks in advance. Quote There is no spoon. <<The Matrix>>
stustarz Posted February 14, 2005 Posted February 14, 2005 Use the string.padright / padleft function e.g: Dim str As String = "String" str.PadRight(10,"s") 'This will return Stringssss - basically it adds the specified character to the right 'of the string, to ensure the total length is 10 - padleft will do the same except 'adds the characters to the start 'You could also declare a new string and use the constructor parameters to make 'a string of the specified character repeated x amount of times: Dim str As New String ("s",10) MessageBox.Show(str) 'This will return the string "ssssssssss" - i think this is more what u r looking for Quote Visit: VBSourceSeek - The VB.NET sourcecode library "A mere friend will agree with you, but a real friend will argue."
Leaders snarfblam Posted February 14, 2005 Leaders Posted February 14, 2005 The string constructor and padleft functions should word for repeating characters. For repeating strings why not just use a for loop that concatenates the string repeatedly? Quote [sIGPIC]e[/sIGPIC]
IngisKahn Posted February 14, 2005 Posted February 14, 2005 StringBuilder = good Concatenate = bad :) Quote "Who is John Galt?"
Leaders snarfblam Posted February 15, 2005 Leaders Posted February 15, 2005 StringBuilder = good Concatenate = bad Good point. I supposed it depends on why you are repeating strings or chars. If it is something simple then there is nothing wrong with concatentation. If you are creating hundreds or more strings in such a fasion (im at a loss for any possible reason) or the strings are excessively long and great in number then yeah, go with string builder. Quote [sIGPIC]e[/sIGPIC]
michael_hk Posted February 15, 2005 Posted February 15, 2005 Thanks for all the input. I know (of course :D ) a simple loop can build the repeating string. But since it is such a common task, I just wonder why there is no built-in function to do this. :eek: like- VBS: String(10, "ABC") PHP: str_repeat Quote There is no spoon. <<The Matrix>>
*Experts* Nerseus Posted February 15, 2005 *Experts* Posted February 15, 2005 I would guess that the constructor of a string that takes a char is there because it was easy to code in assembly - simply pushing the same value into memory x number of times. To shove a string into memory x number of times requires more work and, I would guess, doesn't come up very often. I've never had the need to prefill a string with anything other than a single character. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.