# Modifying strings in a list efficiently

I have a list of strings $\{s_1,s_2,\ldots,s_n\}$. I would like to to produce the following list of strings: $$\{s_1<>1,\ 1<>s_2<>1,\ldots,\ 1<>s_n\}$$ In other words, I want to append the character $1$ to the end of the first string, to either end of all internal strings, and to the beginning of the final string. What is an efficient way to achieve this?

• If efficiency paramount, check my answer, and note that the Riffle based solution will break if strings contain spaces, unless you use a dummy character - which cannot exist in any string - possibly slowing further...
– ciao
Jun 25, 2015 at 5:06

Flatten[
{#[[1]] <> "1", StringInsert[#[[2 ;; -2]], "1", {1, -1}],
"1" <> #[[-1]]}] &@strings

About 2X to over 10X as fast (depending on string lengths) as Riffle et al. that I noted in comment, and unlike that this will return correct results if strings contain spaces. About 4X as fast as mapping (all tested on MMA 9).

• That's the way go to IMO. Jun 25, 2015 at 6:54
• Nice. Can't believe it's that much faster though. Crazy. (+1)
– kale
Jun 25, 2015 at 11:59

Here's a way with v10.1:

strings = {"a", "b", "c", "d", "e"};

StringSplit@StringRiffle[strings, "1 1"]

{"a1", "1b1", "1c1", "1d1", "1e"}

And if you're not on 10.1 (and as suggested by @ciao), you can achieve the same thing with:

StringSplit@StringJoin@Riffle[strings, "1 1"]

And just for the heck of it with MapIndexed:

MapIndexed[If[First@#2 == 1, "", "1"] <> #1 <>
If[First@#2 == Length@strings, "", "1"] &, strings]
• +1 - and a similar construct (I won't post since it's the same in essence) for pre 10.x of StringSplit@StringJoin[Riffle[s, "1 1"]] is considerably faster than mapping solutions....
– ciao
Jun 24, 2015 at 23:40

Pedestrianly,

x = {"a", "b", "c", "d"};

Flatten[{StringJoin[#, "1"] &@First[x],
StringJoin["1", #, "1"] & /@ x[[2 ;; -2]],
StringJoin["1", #] &@Last[x]}]

{"a1", "1b1", "1c1", "1d"}

• Hehe, learned a new adverb just now 😊 Jun 24, 2015 at 21:49
• Like 'prosaic'. (Prose, but not poetry.) Jun 24, 2015 at 22:09