char[][] join ==> string
bearophile
bearophileHUGS at lycos.com
Wed Apr 6 17:13:16 PDT 2011
Given an array of strings std.string.join() returns a single string:
import std.string;
void main() {
string[] a1 = ["hello", "red"];
string j1 = join(a1, " "); // OK
}
But in a program I need an array of mutable arrays of chars. If I join the arrays I get a mutable array of chars. But I need a string:
import std.string;
void main() {
char[][] a2 = ["hello".dup, "red".dup];
string j2 = join(a2, " "); // error
}
Error: cannot implicitly convert expression (join(a," ")) of type char[] to string
.idup avoids the error:
string j3 = join(a2, " ").idup; // OK
Given the low efficiency of the D GC it's better to reduce memory allocations as much as possible.
Here join() creates a brand new array, so idup performs a useless copy. To avoid this extra copy do I have to write another joinString() function?
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list