Easiest way to create a string with n times character 'x'?

From: Arnold Peters (apet10_at_hotmail.com)
Date: 01/27/05


Date: Thu, 27 Jan 2005 20:34:37 +0100

Assume I want to create a string of length n which contains n times the character 'x'.
How do I implement this the easiest way? n could vary from function time to time.

Ok. I could code

char mychar = 'x';
String tmp = "";
for (int i=1; i<n; i++)
    tmp = tmp + String(mychar);
    
But this is very (!) inefficient.

Other solution would be:

String ressource = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String tmp = ressource.subsctring(0,n);

But this is inefficient too and requires a a priori determination of the maximum possible
length;

As far as I know there is a function similar to

String tmp = makeString('x',n);

but I don't remember the exact name.
Can somehelp give me a hint?

Arni



Relevant Pages

  • Re: Easiest way to create a string with n times character x?
    ... > Assume I want to create a string of length n which contains n times the character 'x'. ... n could vary from function time to time. ... > But this is very inefficient. ...
    (comp.lang.java.help)
  • Easiest way to create a string with n times character x?
    ... Assume I want to create a string of length n which contains n times the character 'x'. ... n could vary from function time to time. ... But this is very inefficient. ... String tmp = ressource.subsctring; ...
    (comp.lang.java.programmer)
  • Re: Easiest way to create a string with n times character x?
    ... > Assume I want to create a string of length n which contains n times the character 'x'. ... n could vary from function time to time. ... > But this is very inefficient. ...
    (comp.lang.java.help)
  • Re: Export to CSV problem
    ... properly in IE7 bbut in Mozila it seems the file name is changed and ... string header = string.Empty; ... string body = string.Empty; ...
    (microsoft.public.dotnet.framework.aspnet)
  • Export to CSV problem
    ... string header = string.Empty; ... string body = string.Empty; ... foreach ...
    (microsoft.public.dotnet.framework.aspnet)