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

repeat character

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

Jul 8 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
raulavi wrote:
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.

--
J.
Jul 8 '08 #2

P: n/a
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
Jul 8 '08 #3

P: n/a
String myline = String('*', 100);

Lots of useful constructors for String.

Tom Dacon
Dacon Software Consulting

"raulavi" <ra*****@discussions.microsoft.comwrote in message
news:AE**********************************@microsof t.com...
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

Jul 8 '08 #4

P: n/a
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***
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"raulavi" wrote:
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
Jul 8 '08 #5

P: n/a


"raulavi" <ra*****@discussions.microsoft.comwrote in message
news:AE**********************************@microsof t.com...
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
Jul 8 '08 #6

P: n/a
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)
"raulavi" wrote:
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
Jul 8 '08 #7

P: n/a
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)
"raulavi" wrote:
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
Jul 8 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.