17
$\begingroup$

What is an appropriate command that does the opposite of the following?

StringSplit["a b c d e f g"," "]
$\endgroup$
5
  • $\begingroup$ Yes, shot before aiming ;-) - deleted comment... but it still is broken, e.g. with leading spaces... $\endgroup$
    – ciao
    Mar 27, 2015 at 22:31
  • 1
    $\begingroup$ res = StringSplit[s = " a b c d e f g ", " "]; StringJoin@Riffle[res, " "]; % == s will be False... fails with trailing spaces also. Don't think one can correctly reconstruct all stings from a string split... $\endgroup$
    – ciao
    Mar 27, 2015 at 22:34
  • $\begingroup$ Of course, but then that's not reconstructing the original string s, is it... bottom line, there is no direct inverse for the title form for all strings. $\endgroup$
    – ciao
    Mar 27, 2015 at 22:39
  • 2
    $\begingroup$ Not sure there is one that fits your needs. Adding All as the optional third argument to the split allows correct reconstruction of all strings, but then you will have the "excess" (any leading/trailing/adjacent whitespace) as members of the split. $\endgroup$
    – ciao
    Mar 27, 2015 at 23:04
  • $\begingroup$ Related: (7063), (20412) $\endgroup$
    – Mr.Wizard
    May 20, 2015 at 13:47

5 Answers 5

20
$\begingroup$

A combination of StringJoin and Riffle:

res = StringSplit["a b c d e f g"," "];
StringJoin@Riffle[res," "]
$\endgroup$
8
  • $\begingroup$ you beat me by 10 seconds! I'll delete my answer... $\endgroup$
    – bill s
    Mar 27, 2015 at 17:59
  • $\begingroup$ @bills yep, that was a close one! $\endgroup$
    – C. E.
    Mar 27, 2015 at 18:06
  • $\begingroup$ @WRI, why not just StringJoin[res," "] $\endgroup$ Mar 27, 2015 at 20:28
  • 1
    $\begingroup$ @William StringSplit[" a b c "," "] is just {"a", "b", "c"} so information is lost. There is no way you could know if there were a leading or ending space in the original string. This is the closest to an inverse that you can come. In Javascript that information is preserved by split. $\endgroup$
    – C. E.
    Mar 27, 2015 at 23:44
  • 1
    $\begingroup$ @William ...if you want it to work like your Javascript example you can use StringSplit[" a b c ", " ", All]. $\endgroup$
    – C. E.
    Mar 27, 2015 at 23:48
12
$\begingroup$

In version 10.1 you can use StringRiffle:

res = StringSplit["a b c d e f g", " "];
StringRiffle[res]

Use no second argument for spaces, or something else for something else. A nice advantage of StringRiffle is that res elements can be non string elements, and it will be automatically converted. It's something I miss in StringJoin.

PS: this answer is based on Docs, I don't have V10.1.

Take care with ToString@Row in V9 or older. See this post, about some problems.

$\endgroup$
1
  • $\begingroup$ This fails for example it doesn't match the following js " a b ".split(" ").join(" ") " $\endgroup$
    – William
    Mar 27, 2015 at 22:47
3
$\begingroup$

Sometimes you can you this alternative:

res = StringSplit["a b c d e f g", " "];

ToString @ Row[res, " "]
$\endgroup$
1
$\begingroup$

I know, I know, Kuba's and Pickett's solutions are preferable. For those who fancy Patterns here is an alternative.

   StringReplace[res, t : __ :> t <> " "] // StringJoin  // #~StringDrop~ -1 &  

Doesn't look elegant, anyhow.....it works...hi,hi,hi.!

$\endgroup$
1
$\begingroup$
res = StringSplit["a b c d e f g", " "];    
Fold[#1 <> " " <> #2 &, res]
(*"a b c d e f g"*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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