www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Way to format a string

reply Nils Hensel <nils.hensel web.de> writes:
Hi,

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

Thanks,
Nils
Jan 16 2006
next sibling parent Don Clugston <dac nospam.com.au> writes:
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); }
Jan 16 2006
prev sibling next sibling parent Alex Stevenson <ans104 cs.york.ac.uk> writes:
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
Jan 16 2006
prev sibling parent Nils Hensel <nils.hensel web.de> writes:
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
Jan 16 2006