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.

menjaraz
  • 7,551
  • 4
  • 41
  • 81
dnsmkl
  • 792
  • 5
  • 17

3 Answers3

22
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);
Bernard
  • 45,296
  • 18
  • 54
  • 69
7

Use to from std.conv:

int i = 15
string message = "Value of 'i' is " ~ to!string(i);
eco
  • 2,219
  • 15
  • 20
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.

Michal Minich
  • 2,387
  • 1
  • 22
  • 30