Absolute beginner

Joshua Reusch yoschi at arkandos.de
Fri Jan 13 13:34:28 PST 2012


Am 13.01.2012 22:16, Piotr Szturmaj wrote:
> Jorge wrote:
>> My first question si very silly:
>>
>> string str = readln()
>>
>> my input is for example 123
>>
>> how can i convert this to an integer?
>
> import std.conv;
>
> // then in code:
>
> auto i = to!int(str);

the string returned by readln() ends with NL ('\n'), which causes 
std.conv.to to raise an ConvException. You also must slice it away:

auto i = to!int(str[0..$-1]);

or you use std.conv.parse, which ignores the characters after the value 
and removes the converted characters from the string.


More information about the Digitalmars-d-learn mailing list