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.
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.
Assuming those are lists of strings
Map[StringJoin[#[[1]],"_",#[[2]]]&,{{"abc", "0000"}, {"def", "1111"}}]
returns
{"abc_0000","def_1111"}
Apply
: Apply[StringJoin[{#1, "_", #2}] &, {{"abc", "0000"}, {"def", "1111"}}, {1}]
.
$\endgroup$
Commented
Oct 1, 2019 at 13:35
Map[Curry[StringRiffle]["_"] /* List]
.
$\endgroup$
Commented
Oct 1, 2019 at 13:37
StringJoin[{#1, "_", #2}] & @@@ {{"abc", "0000"}, {"def", "1111"}}
$\endgroup$
Commented
Oct 2, 2019 at 4:06
StringRiffle
is another solution.
Map[StringRiffle[#, "_"] &, {{"abc", "0000"}, {"def", "1111"}}]
{"abc_0000", "def_1111"}
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"} *)
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"}}*)
Using ListFormat
:
strs = {{"abc", "0000"}, {"def", "1111"}};
TextString[#, ListFormat -> {"", "_", ""}] & /@ strs
Using Riffle
:
Riffle[#, "_"] & /@ strs // Map[StringJoin]
Result(s):
{"abc_0000", "def_1111"}
list = {{"abc", "0000"}, {"def", "1111"}};
Using the operator form of Insert
StringJoin @* Insert["_", 2] /@ list
{"abc_0000", "def_1111"}
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"}