Mathematica Stack Exchange is a question and answer site for users of 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

Assume we have a list

list = {"a", "b", "c", "d", "e", "f"}    

Then use this code:


Mathematica graphics

But "*a*b*c*d*e*f*" is expected.So we should

"*" <> StringRiffle[list,"*"] <> "*"

Have a terse method do this?

share|improve this question

closed as off-topic by chuy, blochwave, Edmund, Jens, ilian Mar 30 at 17:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – chuy, blochwave, Edmund, Jens, ilian
If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 9 down vote accepted

StringRiffle can also take strings to prepend and append to the result if you supply three delimiters instead of one. It's not that much terser though:

StringRiffle[list, {"*", "*", "*"}]

Of course you could remove the duplication of the strings with

StringRiffle[list, ConstantArray["*", 3]]

if you like.

For the sake of completeness, the order of the three strings in the list is {prefix, infix, suffix}, e.g.

StringRiffle[list, {"<", "*", ">"}]
(* "<a*b*c*d*e*f>" *)

Alternatively, if you always want to insert some string before/after every character (that is, if the strings in list will never contain two or more characters), you can also use StringReplace:

StringReplace["abcdef", "" -> "*"]
(* "*a*b*c*d*e*f*" *)
share|improve this answer
Very good solution.Cost my some time to research... – yode Mar 23 at 15:33
A workaround come to my mind.StringReplace[StringJoin@list,RegularExpression["\\B|\\b"]->"*"] – yode Mar 23 at 15:46
@yode you can have that more easily with StringReplace["adefgh12c34", "" -> "*"] but of course a lot less flexible since StringRiffle's first list may contain strings that are longer than a single character. I'll add this though if it's useful to you. – Martin Ender Mar 23 at 15:48
You help me once again.. – yode Mar 23 at 15:49

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