repeat character

  • Thread starter Thread starter raulavi
  • Start date Start date
R

raulavi

Hi:
vs2008 c#
Having a string myvar = "*";

I need to repeat this myvar 100 times when is used at run time...

string myline = myvar + myvar+ myvar....(100 times)

What the easiest way to acomplish this.

am trying to avoid the for loop
 
raulavi said:
Hi:
vs2008 c#
Having a string myvar = "*";

I need to repeat this myvar 100 times when is used at run time...

string myline = myvar + myvar+ myvar....(100 times)

What the easiest way to acomplish this.

am trying to avoid the for loop
Well, then,

myline =
"****************************************************************************************************";

should do, right? :-)

There's nothing wrong with a for loop, you just need to avoid excessive
string creation and memory allocation. You can accomplish that with
StringBuilder:

StringBuilder b = new StringBuilder(myvar.Length * 100);
for (int i = 0; i != 100; ++i) {
b.Append(myvar);
}
string myline = b.ToString();

If "myvar" will always be a single character, you can use the
StringBuilder.Append() override that's especially for this.
 
is it a string or a char?

For example, one option is:

string s = new string('*', 100);

if it is genuinely a string (and might be multiple characters), then
something like:

static string Repeat(string input, int count)
{
StringBuilder sb = new StringBuilder(input.Length * count);
for (int i = 0; i < count; i++)
{
sb.Append(input);
}
return sb.ToString();
}

Marc
 
String myline = String('*', 100);

Lots of useful constructors for String.

Tom Dacon
Dacon Software Consulting
 
Either:
myvar = myvar.PadLeft(100, myvar[0]);
or:
myvar = String('*',100);
***remember to use single quotes on that as it takes a char not a string***
 
raulavi said:
Hi:
vs2008 c#
Having a string myvar = "*";

I need to repeat this myvar 100 times when is used at run time...

string myline = myvar + myvar+ myvar....(100 times)

What the easiest way to acomplish this.

am trying to avoid the for loop

Multiple answers and so far, all can be correct, so I thought I'd throw in
another case...

If the string or "character" is just a space (which in your example, it
showed an asterisk [*], but I'm throwing in a space just for show):

string myvar = string.Empty.PadRight(100);

- OR - a normal character (note, char is not string):

string myvar = string.Empty.PadRight(100, '*');

Personally, I would use the constructor for string that takes a character
and count (already posted by someone else, but duplicated here):

string myvar = new string('*', 100);


There ;) HTH,
Mythran
 
thanks to all...
I like
string s = new string('*', 100);

the s will be filled with 100 '*' at run time
(the char is known only at run time)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top