views:

131

answers:

5

This question has been asked so many times for so many different languages, with some fascinating answers. I propose compiling the answers here so we can compare them side by side.

Also, how is it done in the languages not yet "covered" by a question on SO?

One programming language or idiom per answer, please.

+1  A: 

PostScript:

/char (A) def      % the character, must be a 1-char string in the first solution
/n 29 def          % the count

/str n string def  %the result string

0 1 n 1 sub {str exch char putinterval} for

% Alternative:
% char 0 get
% 0 1 n 1 sub {str exch 2 index put} for pop

/Helvetica findfont
12 scalefont setfont
20 720 moveto
(Here's your string: ) show
str show showpage
balpha
+1  A: 

Perl:

"x" x 10

is all.

Alex Brown
A: 

I just love Jason Orendorff's aforelinked solution for JavaScript:

Array(11).join("a")

It also works for Ruby:

Array.new(10).join("a")

Any other languages where this idiom works?

Yang
In Tcl, there is no constructor for lists to generate a list with $n empty elements. But with the inverse of this idiom you can do it: `set newList [split [string repeat " " 9]]`
slebetman
`'a'.join(['']*11)` in Python, but why would you?
gnibbler
As a side comment, in Python you could just do `'a' * 10`.
Xavier Ho
+1  A: 

Tcl

string repeat x 10 ; # repeats the character x 10 times
slebetman
A: 

c#

string x = new String('x', 50);
wefwfwefwe