Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

integer to string conversion in D

Tags:

casting

d

phobos

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.

like image 995
dnsmkl Avatar asked May 24 '12 17:05

dnsmkl


People also ask

How do you convert integers to strings?

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.

Is a method used to convert the data into string form?

valueOf(int) String. valueOf() is static utility method of String class that can convert most primitive data types to their String representation.

How do I convert an int to a string in Python?

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.


2 Answers

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);
like image 191
Bernard Avatar answered Oct 06 '22 01:10

Bernard


Use to from std.conv:

int i = 15
string message = "Value of 'i' is " ~ to!string(i);
like image 22
eco Avatar answered Oct 06 '22 00:10

eco