3
$\begingroup$

Is there a way to use StringJoin to go from:

{{abc, 0000}, {def, 1111}}

To

{{abc_0000},{def_1111}}

I've been trying to use Map and MapThread with StringJoin but have failed.

$\endgroup$
1
  • $\begingroup$ One way I've discovered is by using Transpose[] first, then MapThread[]. If there are any other solutions I'd appreciate it. $\endgroup$
    – reemodels
    Commented Oct 1, 2019 at 12:54

7 Answers 7

4
$\begingroup$

Assuming those are lists of strings

Map[StringJoin[#[[1]],"_",#[[2]]]&,{{"abc", "0000"}, {"def", "1111"}}]

returns

{"abc_0000","def_1111"}
$\endgroup$
3
  • 1
    $\begingroup$ Alternatively, one may use Apply: Apply[StringJoin[{#1, "_", #2}] &, {{"abc", "0000"}, {"def", "1111"}}, {1}]. $\endgroup$ Commented Oct 1, 2019 at 13:35
  • 1
    $\begingroup$ For a points-free (point-less?) solution, you can try Map[Curry[StringRiffle]["_"] /* List]. $\endgroup$ Commented Oct 1, 2019 at 13:37
  • 1
    $\begingroup$ @HenrikSchumacher - or the abbreviated form: StringJoin[{#1, "_", #2}] & @@@ {{"abc", "0000"}, {"def", "1111"}} $\endgroup$
    – Bob Hanlon
    Commented Oct 2, 2019 at 4:06
5
$\begingroup$

StringRiffle is another solution.

Map[StringRiffle[#, "_"] &, {{"abc", "0000"}, {"def", "1111"}}]

{"abc_0000", "def_1111"}

$\endgroup$
2
$\begingroup$

Pattern matching also comes in handy:

strs = {{"abc", "0000"}, {"def", "1111"}};

Cases[strs, {a_, b_} :> StringJoin[a , "_", b]]
 (* {"abc_0000", "def_1111"} *)

Replace[strs, {a_, b_} :> StringJoin[a , "_", b], {1}]
 (* {"abc_0000", "def_1111"} *)
$\endgroup$
2
$\begingroup$
list = {{"abc", "0000"}, {"def", "1111"}};

Using SequenceCases:

SequenceCases[list, {{a_, b_}} :> {StringJoin[a, "_", b]}]

(*{{"abc_0000"}, {"def_1111"}}*)

A variant with Cases and Insert:

Cases[list, x_ :> {StringJoin@Insert[x, "_", 2]}]

(*{{"abc_0000"}, {"def_1111"}}*)

Or using MapThread:

MapThread[{#1 <> "_" <> #2} &, Thread[list]]

(*{{"abc_0000"}, {"def_1111"}}*)
$\endgroup$
2
$\begingroup$

Using ListFormat:

strs = {{"abc", "0000"}, {"def", "1111"}};

TextString[#, ListFormat -> {"", "_", ""}] & /@ strs

Using Riffle:

Riffle[#, "_"] & /@ strs // Map[StringJoin]

Result(s):

{"abc_0000", "def_1111"}

$\endgroup$
1
  • $\begingroup$ You don't need the Map[StringJoin] $\endgroup$
    – eldo
    Commented May 22 at 22:05
2
$\begingroup$
list = {{"abc", "0000"}, {"def", "1111"}};

Using the operator form of Insert

StringJoin @* Insert["_", 2] /@ list

{"abc_0000", "def_1111"}

$\endgroup$
1
$\begingroup$
list = {{"abc", "0000"}, {"def", "1111"}};

Using SequenceReplace (new in 11.3)

SequenceReplace[list, {{a_, b_}} :> StringJoin[a, "_", b]]

{"abc_0000", "def_1111"}

Using ReplaceAt (new in 13.1)

ReplaceAt[x_ :> StringRiffle[x, "_"], All] @ list

{"abc_0000", "def_1111"}

Using ReplaceAll

list /. x :{__String} :> StringRiffle[x, "_"]

{"abc_0000", "def_1111"}

$\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.