How to make a formatted string ?
Lars T. Kyllingstad
public at kyllingen.NOSPAMnet
Wed Mar 17 06:58:20 PDT 2010
Gabriel Laskar wrote:
> On 03/17/2010 12:59 PM, Gabriel Laskar wrote:
>> Hi,
>>
>> I am searching how to do a formatted string like with sprintf()
>>
>> I have found std.format.formattedWrite() but when I try :
>>
>> Appender!(string) msg;
>> formattedWrite(msg, "toto: %0", 42);
>> writeln(msg);
>>
>> It fails with :
>> core.exception.RangeError@�(1582): Range violation
>>
>> I have also found std.string.format, but it seems to fail also.
>
> Oops, I'm stupid, It is not %0, but %s... std.string.format is good.
> Now I need to pass an array of char[] instead of va_args, I have found
> a work around, but it seems bad :
>
> string formatArray(char[][] args)
> {
> switch (args.length)
> {
> case 1: return format(args[0]);
> case 2: return format(args[0], args[1]);
> // ...
> }
> }
>
> If someone have something better...
It depends on what you are trying to do.
char[][] a = ["hello".dup, "world".dup];
string fmt = format(a);
// fmt is now "[hello,world]"
char[][] a = ["hello %s %s world".dup, "foo".dup, "bar".dup];
string fmt = format(a[0], a[1 .. $]);
// fmt is now "hello foo bar world"
-Lars
More information about the Digitalmars-d-learn
mailing list