Considering a list:
lst = {"1", "2", "3", "4", "5", "6"}
How can I have two strings :
str1 = "1, 2, 3"
str2 = "4, 5, 6"
Thanks!
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityConsidering a list:
lst = {"1", "2", "3", "4", "5", "6"}
How can I have two strings :
str1 = "1, 2, 3"
str2 = "4, 5, 6"
Thanks!
StringTake[ToString@#, {2, -2}] & /@ Partition[lst, 3]
{"1, 2, 3", "4, 5, 6"}
Assuming you always want strings with 3 items:
Map[StringJoin @@ Riffle[#, ", "] &, Partition[Map[ToString, lst], 3]]
This is very similar to your previous question and so is the solution:
lst = {"1", "2", "3", "4", "5", "6"}
ToString @ Row[#, ", "] & /@ Partition[lst, 3]
{"1, 2, 3", "4, 5, 6"}
lst = {"1", "2", "3", "4", "5", "6"};
Developer`PartitionMap[StringTrim[ToString@#, "{" | "}"] &, lst, 3]
(* {"1, 2, 3", "4, 5, 6"} *)
This gives exactly the output you wrote down:
str1 = StringDrop[StringDrop[ToString@Partition[lst, 3][[1]], 1], -1]
str2 = StringDrop[StringDrop[ToString@Partition[lst, 3][[2]], 1], -1]
Could be made more elegant of course, and more flexible.
EDIT to make it more elegant:
TableForm[StringJoin /@ Partition[lst, 3]]
for display without the commas.
str1 = StringJoin @@ lst[[ ;; 3]]
$\endgroup$ – C. E. Apr 15 '15 at 12:01