type conversions

Era Scarecrow rtcvb32 at yahoo.com
Sun Apr 29 17:19:44 PDT 2012


On Sunday, 29 April 2012 at 23:42:39 UTC, 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;
>
>
> 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.

I think this is right...

import std.stdio;
import std.conv;

int main(string args[]){
	string str = "Hello world!";
	char[] c = to!(char[])(str);
	
	string num = "1234";
	int i = to!int(num);
	
	writefln("%s\t- %s", typeid(str), str);
	writefln("%s\t- %s", typeid(num), num);

	writefln("%s\t- %s", typeid(c), c);
	writefln("%s\t- %s", typeid(i), i);

return 0;
}



More information about the Digitalmars-d-learn mailing list