On Wednesday, 15 June 2022 at 04:26:44 UTC, Salih Dincer wrote:
Hi,

I've been interested in conversion possibilities for a while. I tried to convert a string containing numbers but with no success in single digits. The only solution I found is to subtract 48 from the result:

```d
import std;
void main()
{
    string str = "abc123";
    str[3..$].to!int.writeln; // 123
    auto bar = str[3].to!int;
    assert(bar == 49); // 49 is value for ASCII.
    writeln(bar - 48); // 1
}
```

By indexing `str`, you're getting a `char`. So `to` is operating on that rather than on a string. Slicing will give you what you want, since then you'd have a `"1"` rather than a `'1'`:

```d
str[3..4].to!int;
```

Reply via email to