How to catenate a string multiple times ?
H. S. Teoh
hsteoh at quickfur.ath.cx
Sat Mar 16 10:26:15 PDT 2013
On Sat, Mar 16, 2013 at 04:29:02PM +0100, Peter Sommerfeld wrote:
> Cannot find a reference: What is the best way
> to catenate a string multiple times ?
> Unfortunately this this does not work ;-)
>
> string tab = "..";
> tab = tab * 4; // -> "........"
[...]
You could define something like this:
string x(string a, int n) {
import std.array;
auto app = appender!string(a);
while (--n) {
app.put(a);
}
return app.data;
}
void main() {
// prints "abcabcabc"
writeln("abc".x(3));
}
It's not as nice as "abc"*3, but unfortunately opBinary only works when
declared inside the class/struct; UFCS doesn't pick it up, so it doesn't
work on native types.
T
--
Why do conspiracy theories always come from the same people??
More information about the Digitalmars-d-learn
mailing list