integer to string conversion in D
Asked Answered
C

3

14

How in D would I cast integer to string? Something like

int i = 15
string message = "Value of 'i' is " ~ toString(i); // cast(string) i - also does not work 

Google brought me the answer on how to do it with tango, but I want the phobos version.

Collage answered 24/5, 2012 at 17:29 Comment(0)
H
23
import std.conv;

int i = 15;
string message = "Value of 'i' is " ~ to!string(i);

or format:

import std.string;
string message = format("Value of 'i' is %s.", i);
Hasa answered 24/5, 2012 at 17:32 Comment(0)
A
7

Use to from std.conv:

int i = 15
string message = "Value of 'i' is " ~ to!string(i);
Apical answered 24/5, 2012 at 17:31 Comment(0)
A
3
import std.conv;
auto i = 15;
auto message = text("Value of 'i' is ", i);

there are also wtext an dtext variants witch returns wstring and dstring.

Avail answered 27/5, 2012 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.