type conversions
Jonathan M Davis
jmdavisProg at gmx.com
Sun Apr 29 18:08:13 PDT 2012
On Monday, April 30, 2012 01:42:38 WhatMeWorry wrote:
> I'm trying to get my head around D's type conversion. What is the
> best way to convert a string to a char array? Or I should say is
> this the best way?
>
> string s = "Hello There";
> char[] c;
>
> c = string.dup;
dup will return a mutable copy of an array. idup will return an immutable copy
of an array. They will both always copy. If you want to convert without having
to make a copy if the array is of the constancy that you want already (e.g. if
a templated function is templated on string type, and it could be any
constancy of char[]), then use std.conv.to.
auto c = to!(char[])(str);
If str was already char[], then it will just be returned, whereas if it's
immutable(char)[], then it would dup it and return that.
> Also, what is the best way to explicitly convert a string to an
> int? I've been looking at Library Reference (Phobos) but I'm
> stuck.
Use std.conv.to:
auto i = to!string("1234");
std.conv.to is what you use for pretty much any conversion.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list