Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a variable length string of whitespaces in Mathematica

The following Mathematica function f creates a string of whitespace of length n.

f[n_]:=Fold[StringJoin,"",Array[" "&,n]]

There must be a zillion alternatives to create this function.

How would you have done it?

like image 757
nilo de roock Avatar asked May 21 '11 13:05

nilo de roock


2 Answers

f[n_] := StringJoin @ ConstantArray[" ", n]

Edit: since @ is as idiomatic as @@ and a bit faster (thanks to Mr.Wizard for benchmarking) and shorter i updated the solution.

like image 185
Thies Heidecke Avatar answered Nov 11 '22 03:11

Thies Heidecke


f[n_] := FromCharacterCode[ConstantArray[32, {n}]]

By the way: you should be aware that this type of question is frowned upon in the faq:

What kind of questions should I not ask here?

You should only ask practical, answerable questions based on actual problems that you face. Chatty, open-ended questions diminish the usefulness of our site and push other questions off the front page. To prevent your question from being flagged and possibly removed, avoid asking subjective questions where …

1. every answer is equally valid: “What’s your favorite ______?”

Don't be surprised if the question is closed.

like image 5
Sjoerd C. de Vries Avatar answered Nov 11 '22 02:11

Sjoerd C. de Vries