Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Tue, 8 Jul 2008 08:06:06 -0700,    group: microsoft.public.dotnet.languages.csharp        back       


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
date: Tue, 8 Jul 2008 08:06:06 -0700   author:   raulavi

Re: repeat character   
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.
date: Tue, 08 Jul 2008 17:14:18 +0200   author:   Jeroen Mostert

Re: repeat character   
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
date: Tue, 8 Jul 2008 16:20:32 +0100   author:   Marc Gravell

Re: repeat character   
String myline = String('*', 100);

Lots of useful constructors for String.

Tom Dacon
Dacon Software Consulting

"raulavi"  wrote in message 
news:AE262A06-16D6-405B-82AC-281267EE3E82@microsoft.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
>
date: Tue, 8 Jul 2008 08:24:18 -0700   author:   Tom Dacon am

RE: repeat character   
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
>
date: Tue, 8 Jul 2008 09:16:13 -0700   author:   Ciaran O''Donnell

Re: repeat character   
"raulavi"  wrote in message 
news:AE262A06-16D6-405B-82AC-281267EE3E82@microsoft.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
date: Tue, 8 Jul 2008 09:21:25 -0700   author:   Mythran am

RE: repeat character   
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
>
date: Tue, 8 Jul 2008 15:08:01 -0700   author:   raulavi

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us