Absolute beginner

Matej Nanut matejnanut at gmail.com
Fri Jan 13 13:40:46 PST 2012


You could also try to!int(str.strip), strip() removes
whitespace from the left and right of a string.
You have to import std.string for it.

On 13 January 2012 22:34, Joshua Reusch <yoschi at arkandos.de> wrote:
> 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