Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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!
  • 1 year later...
Posted

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

Visit: VBSourceSeek - The VB.NET sourcecode library

 

 

"A mere friend will agree with you, but a real friend will argue."
  • Leaders
Posted
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?
[sIGPIC]e[/sIGPIC]
  • Leaders
Posted
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.

[sIGPIC]e[/sIGPIC]
Posted

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

There is no spoon. <<The Matrix>>
  • *Experts*
Posted

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

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...