473,473 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

repeat character

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
7 5094
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
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
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
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


"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Boris ©avc | last post by:
How to change character in a file from 'a' to 'b' (g++, linux)? Thanks for the pointers. Regards, Boris
8
by: Agnes | last post by:
In my .net ,i need to generate an xml file , however, user may input a chinese character, Then , the xml will got something unknow characters. the following is my code, Does anyone know how to...
3
by: PC Datasheet | last post by:
I need a textbox to be set to a string of 15 Xs. Is there some expression that does something like the following pseudocode: MyTextbox = "X"(15) If there is, can it be done with a multiple...
13
by: Peter Monsson | last post by:
Hi all, I'm sitting with a problem where I have some special characters which have been "stringified" meaning that instead of having f. x. a '\n' or '\x20' I have "\n" and "\x20" and I have to...
3
by: John Sutor | last post by:
I want to write a line to a text file that looks like this ----------------------------------------------- How can I do this without doing this manually and assigning it to a variable. Ex. string...
5
by: Raterus | last post by:
I think I've seen this before as a built-in function. I need a function that I pass it a character and a number and it returns that character repeated that many times. Easy enough to do myself, I...
5
by: John L. Whelan | last post by:
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...
3
by: Sergei Shelukhin | last post by:
Can someone tell me why does IE 6 ignore no-repeat and repeat bg image twice in the following page? I tried adding it to container div instead of form, but it is till repeated exactly twice. With...
44
by: shahaba | last post by:
I have a list of texts and I need to find out the number of times each character shows up. Example in the text "76DC4-34" the character '4' shows up twice. Is there a query that for this instance...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.