5
$\begingroup$

Is there a way to write down Mathematica with a string such as:
{"M","a","t","h","e","m","a","t","i","c","a"}?

$\endgroup$
1
  • 3
    $\begingroup$ ?*String* will list the available string functions if you need more information. $\endgroup$ – image_doctor Jun 19 '12 at 19:47
14
$\begingroup$

Supporting the maxim "there is always another way to do it":

list = {"M","a","t","h","e","m","a","t","i","c","a"};

StringJoin accepts lists directly, and in fact is faster this way:

StringJoin @ list
"Mathematica"

Also, StringJoin has the short form <> therefore you could also use:

"" <> list
"Mathematica"

Speed check:

large = Characters@ExampleData[{"Text", "LoremIpsum"}];

Do[StringJoin @@ large, {5000}] // Timing

Do[StringJoin @ large, {5000}]  // Timing

Do["" <> large, {5000}]         // Timing

Version 7.0.1 timings:

{1.622, Null}

{0.702, Null}

{0.718, Null}

Version 10.1.0 timings:

{0.6864, Null}

{0.4524, Null}

{0.4524, Null}
$\endgroup$
10
  • $\begingroup$ Good maxim :) You and Jagra seem to have the same one :) $\endgroup$ – Öskå Jun 19 '12 at 19:21
  • 3
    $\begingroup$ ""<> list ...Elegant! $\endgroup$ – Jagra Jun 19 '12 at 19:24
  • $\begingroup$ @Öskå Jagra's answer shows something else: it would still work if StringJoin could only accept two strings. Fold is a very useful function to know. See my example here. $\endgroup$ – Mr.Wizard Jun 19 '12 at 19:25
  • $\begingroup$ @Jagra thanks :D $\endgroup$ – Mr.Wizard Jun 19 '12 at 19:25
  • 1
    $\begingroup$ It's a shame that you give the nice answer StringJoin @ list, but then use the not-so-nice answer StringJoin @@ list in your speed comparison. $\endgroup$ – Carl Woll Oct 10 '19 at 19:56
10
$\begingroup$

Just for something different Fold works too:

Fold[#1 <> #2 &, "", {"M", "a", "t", "h", "e", "m", "a", "t", "i", 
  "c", "a"}]

"Mathematica"
$\endgroup$
9
$\begingroup$

Use StringJoin:

StringJoin @@ {"M", "a", "t", "h", "e", "m", "a", "t", "i", "c", "a"}

"Mathematica"

$\endgroup$
2
  • $\begingroup$ ah, been looking for one hour. Feels dumb Thanks! $\endgroup$ – Öskå Jun 19 '12 at 18:41
  • 3
    $\begingroup$ No problem, we've all experienced the same thing, good luck. $\endgroup$ – image_doctor Jun 19 '12 at 18:42
7
$\begingroup$

If we're going to get silly about it:

TextRecognize @ Graphics @ MapIndexed[Text[Style[#1, 50], {#2[[1]], 0}] &, list]
$\endgroup$
0
5
$\begingroup$

At risk of taking "even more" too far here is a method that does not use any function with "String" in the name:

list = {"M", "a", "t", "h", "e", "m", "a", "t", "i", "c", "a"};

FromCharacterCode @ Flatten @ ToCharacterCode @ list

Or using an export function:

ExportString[{list}, "Table", "FieldSeparators" -> ""]

Or a formatting function:

ToString @ Row @ list
$\endgroup$
2
  • $\begingroup$ Sheer vulpinism, that's what it is :) $\endgroup$ – István Zachar Jun 19 '12 at 21:37
  • $\begingroup$ +1 for: 'ToString @ Row @ list'. This seems to me the most interesting of all the solutions offered for the question, because you've taken such an original view of the problem while maintaing your customary clarity and conciseness. Nice! $\endgroup$ – Jagra Jun 20 '12 at 14:03
4
$\begingroup$

Just to stretch even more the limits of "there is always another way to do it" (Mr.Wizard), here is a way to NOT use StringJoin at all:

chars = {"M", "a", "t", "h", "e", "m", "a", "t", "i", "c", "a"};
Fold[StringInsert[#1, #2, -1] &, "", Flatten@chars]
"Mathematica"

And a simpler one:

StringExpression @@ chars

Using built-in path functions to complicate things (use ToFileName if prior to version 7):

StringReplace[FileNameJoin@chars, "\\" -> ""]
$\endgroup$
0
4
$\begingroup$

In versions 10.1+, there is also StringRiffle:

StringRiffle[list, ""]

"Mathematica"

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.