Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a list:

lis = {"ab", "cde", 2,"rs",3,"tu","v","w",7}

and would like to get the following list:

lis2 = {abcde, 2,rs, 3,tuvw,7}

... with adjacent strings joined and separated by integers.

Any thoughts would be appreciated!

share|improve this question
up vote 4 down vote accepted

If you don't mind code that generates messages, then the following will work:

Quiet[List@@StringJoin[lis],StringJoin::string]

(* {abcde,2,rs,3,tuvw,7} *)
share|improve this answer
    
Excellent! Thanks – Suite401 4 hours ago
lis = {"ab", "cde", 2, "rs", 3, "tu", "v", "w", 7};

lis //. {s___, str1_String, str2_String, f___} :>
  {s, StringJoin[str1, str2], f}

(*  {"abcde", 2, "rs", 3, "tuvw", 7}  *)

or

lis //. {s___, str1_String, str2_String, f___} :>
  {s, str1 <> str2, f}
share|improve this answer

This is a bit elaborate:

If[VectorQ[#, StringQ], StringJoin[#], Sequence @@ #] & /@ SplitBy[lis, StringQ]
   {"abcde", 2, "rs", 3, "tuvw", 7}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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