Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Leave Comments, Critiques, and Suggestions Here?

Text Conversions Tutorial

Text conversions are made easy in tango through a common set of interfaces to convert to and from text. The 'parse' method converts from text to some type, and the 'format' method converts from some type to its textual representation. 'format' 's second argument is a pre-allocated buffer for conversion . Numbers are an often converted category so we will start with those:

Number Conversions

lightweight numeric conversions are located in modules tango.text.convert.Integer and tango.text.convert.Float:

// from text to numbers
int i = Integer.parse("302");
real d = Float.parse("3.14159");

// and from numbers to text
char[] intString = Integer.toUtf8(32);
char[] floatString = Float.toUtf8(3.14159);


We will continue with the other most common types, which would be converting between different unicode representations. Sticking to tango tradition of using 'toUtf8' to mean 'give me a string representation' , tango.text.conversion.Unicode contains templated methods to convert between different encodings including:

char[] eight = Unicode.toUtf8("test");
wchar[] sixteen  = Unicode.toUtf16("test");
dchar[] thirtyTwo = Unicode.toUtf32("test");


Probably the most useful class in the conversion line-up is the 'Layout' class, which formats a string based on C# style syntax.

auto layout = new Layout!(char);
char[] str = layout ("{} sells {} by the {}","Sally", "shells", "seashore");

For a full tutorial on formatting see The C# Formatter Tutorial.


The tango.text.convert package contains a set of classes for everyday numeric convertion (Float, Integer). The convention of using parse() and format() among all the classes makes it easy to just dive right in with a conversion class.

Other more exotic classes exist for converting dates, handling Unicode BOM, etc. They can be located via the Reference Manual - Conversions chapter.