0
$\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$
  • $\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 Oct 1 '19 at 12:54
2
$\begingroup$

Assuming those are lists of strings

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

returns

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

StringRiffle is another solution.

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

{"abc_0000", "def_1111"}

| improve this answer | |
$\endgroup$
1
$\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"} *)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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