Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 16, 2006 Way to format a string | ||||
---|---|---|---|---|
| ||||
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 Re: Way to format a string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nils Hensel | 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 Re: Way to format a string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nils Hensel | 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 Re: Way to format a string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nils Hensel | 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
|
Copyright © 1999-2021 by the D Language Foundation