9
$\begingroup$

I'm having some trouble using StringJoin --- the problem is that the StringJoin function creates a string where all the words in a list are combined without any spaces (whitespace)

For instance, list = {"hi","how","are","you"} StringJoin[list] will give me hihowareyou What do I have to do to get hi how are you as one string?

$\endgroup$
5
$\begingroup$

Another option is to use ListFormat

  TextString[list, ListFormat -> {"", "    ", ""}]

gives

   "hi    how    are    you"
$\endgroup$
1
  • $\begingroup$ Thank you for this! All of the answers here worked but I found this to be the simplest $\endgroup$ Oct 13 '17 at 16:33
10
$\begingroup$

In M10.1+ you can use StringRiffle:

StringRiffle[
    {"hi","how","are","you"},
    " "
] //InputForm

"hi how are you"

$\endgroup$
3
$\begingroup$

The Map function will "do the same thing" to every element in a list. So we use that to StringJoin a space onto the end of every word in your list, except the last word. And then we Use StringJoin on that plus the last word in your list to put all those together into a single string. So

list = {"hi", "how", "are", "you"};
StringJoin[Map[StringJoin[#, " "]&, Most[list]], Last[list]]

which gives you the string "hi how are you"

$\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.