import std.algorithm, std.array, std.string;
string[] foo = ["zxc", "asd", "qwe"];
string str = foo.map!(a => format(`"%s"`, a)).join(", ");
assert(str == `"zxc", "asd", "qwe"`);
std.algorithm.map
takes a lambda which converts an element in the range into something else, and it returns a lazy range where each element is the result of passing an element from the original range to the lambda function. In this case, the lambda takes the input string and creates a new string which has quotes around it, so the result of passing foo
to it is a range of string
s which have quotes around them.
std.array.join
then takes a range and eagerly concatenates each of its elements with the given separator separating each one and returns a new array, and since it's given a range of string
s it returns a string
. So, by giving it the result of the call to map
and ", "
for the separator, you get your string made up of the original strings quoted and separated by commas.
std.algorithm.joiner
would do the same thing as join
, except that it results in a lazy range instead of an array.