www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Multiple %s format specifiers with a single argument

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
import std.string;

void main()
{
    string foo = "foo";
    string bar = format("%s %s %s", foo);
}

format expects 3 arguments, but what I really want is foo to be used
for all 3 specifiers and not repeat 'foo' 3 times manually. Are there
any format specifiers that do what I want? I've tried using positional
specifiers but that didn't work:

string bar = format("%1$s %1$s %1$s", foo);
$ std.format.FormatException std\format.d(4363): string

That's a great error message btw.
Apr 09 2012
parent reply "q66" <quaker66 gmail.com> writes:
On Monday, 9 April 2012 at 17:09:03 UTC, Andrej Mitrovic wrote:
 import std.string;

 void main()
 {
     string foo = "foo";
     string bar = format("%s %s %s", foo);
 }

 format expects 3 arguments, but what I really want is foo to be 
 used
 for all 3 specifiers and not repeat 'foo' 3 times manually. Are 
 there
 any format specifiers that do what I want? I've tried using 
 positional
 specifiers but that didn't work:

 string bar = format("%1$s %1$s %1$s", foo);
 $ std.format.FormatException std\format.d(4363): string

 That's a great error message btw.
Positional specifier works just fine for me.
Apr 09 2012
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 4/9/12, q66 <quaker66 gmail.com> wrote:
 Positional specifier works just fine for me.
Which version are you using? I'm on 2.058.
Apr 09 2012
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/09/2012 10:24 AM, Andrej Mitrovic wrote:
 On 4/9/12, q66<quaker66 gmail.com>  wrote:
 Positional specifier works just fine for me.
Which version are you using? I'm on 2.058.
Positional parameters[*] are supported in 2.058. This example prints the same argument in decimal, hexadecimal, octal, and binary: writefln("%1$d %1$x %1$o %1$b", 42); Ali [*] http://ddili.org/ders/d.en/formatted_output.html
Apr 09 2012
prev sibling parent "q66" <quaker66 gmail.com> writes:
On Monday, 9 April 2012 at 17:24:35 UTC, Andrej Mitrovic wrote:
 On 4/9/12, q66 <quaker66 gmail.com> wrote:
 Positional specifier works just fine for me.
Which version are you using? I'm on 2.058.
git
Apr 09 2012