Thread overview
Way to format a string
Jan 16, 2006
Nils Hensel
Jan 16, 2006
Don Clugston
Jan 16, 2006
Alex Stevenson
Jan 16, 2006
Nils Hensel
January 16, 2006
Hi,

I'd like to know if there's a D way to format a char[] the way printf does?

Thanks,
Nils
January 16, 2006
Nils Hensel wrote:
> Hi,
> 
> I'd like to know if there's a D way to format a char[] the way printf does?
> 
> Thanks,
> Nils

import std.string;
import std.stdio;

void main()
{
int x=10;
real y = 1.0/17;
char [] s = format("x=", x, "y = %.8f", y);
writefln(s);
}
January 16, 2006
Nils Hensel wrote:
> Hi,
> 
> I'd like to know if there's a D way to format a char[] the way printf does?
> 
> Thanks,
> Nils

std.string.format does this very well:

import std.string;

void somefunc()
{
    char[] aString = format( "String! %d%f%s", 10, 10.0f, "foo" );

    //The D style use is also supported:

    char[] bString = format( "String! ", 10, 10.0f, "foo" );

    //aString and bString are equal...
}

There's also sformat for a sprintf style call where you pass a char[] buffer as the first argument
January 16, 2006
Nils Hensel schrieb:
> Hi,
> 
> I'd like to know if there's a D way to format a char[] the way printf does?
> 
> Thanks,
> Nils


Thanks a lot! It's not yet part of Skys docwiki so I missed that one :[
Guess I have to look up the original docs more often.

Regards,
Nils