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.
The easiest way to convert int to String is very simple. Just add to int or Integer an empty string "" and you'll get your int as a String. It happens because adding int and String gives you a new String. That means if you have int x = 5 , just define x + "" and you'll get your new String.
valueOf(int) String. valueOf() is static utility method of String class that can convert most primitive data types to their String representation.
To convert an integer to a string, use the str() built-in function. The function takes an integer (or other type) as its input and produces a string as its output.
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);
Use to
from std.conv:
int i = 15
string message = "Value of 'i' is " ~ to!string(i);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With