17

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

StringSplit["a b c d e f g"," "]
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
William
  • 7,595
  • 2
  • 22
  • 70
  • Yes, shot before aiming ;-) - deleted comment... but it still is broken, e.g. with leading spaces... – ciao Mar 27 '15 at 22:31
  • 1
    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... – ciao Mar 27 '15 at 22:34
  • 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. – ciao Mar 27 '15 at 22:39
  • 2
    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. – ciao Mar 27 '15 at 23:04

5 Answers5

20

A combination of StringJoin and Riffle:

res = StringSplit["a b c d e f g"," "];
StringJoin@Riffle[res," "]
C. E.
  • 70,533
  • 6
  • 140
  • 264
  • you beat me by 10 seconds! I'll delete my answer... – bill s Mar 27 '15 at 17:59
  • @bills yep, that was a close one! – C. E. Mar 27 '15 at 18:06
  • @WRI, why not just StringJoin[res," "] – alancalvitti Mar 27 '15 at 20:28
  • ...& operator form StringJoin["/"] eg list of strings --> path – alancalvitti Mar 27 '15 at 20:28
  • @alancalvitti Turns out WRI has fixed this in 10.1, see Murta's answer. – C. E. Mar 27 '15 at 20:55
  • this fails to do the inverse. For example it doesn't match the output of the following js " a b ".split(" ").join(" ") – William Mar 27 '15 at 22:46
  • 1
    @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. – C. E. Mar 27 '15 at 23:44
  • 1
    @William ...if you want it to work like your Javascript example you can use StringSplit[" a b c ", " ", All]. – C. E. Mar 27 '15 at 23:48
12

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.

Murta
  • 26,275
  • 6
  • 76
  • 166
3

Sometimes you can you this alternative:

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

ToString @ Row[res, " "]
Kuba
  • 136,707
  • 13
  • 279
  • 740
1

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.!

penguin77
  • 1,645
  • 9
  • 8
1
res = StringSplit["a b c d e f g", " "];    
Fold[#1 <> " " <> #2 &, res]
(*"a b c d e f g"*)
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78