Skip to main content

concatenation - Getting a String from a list of strings


Is there a way to write down Mathematica with a string such as:

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



Answer



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}

Comments