How to remove all characters from a string, except the integers?

Salih Dincer salihdb at hotmail.com
Fri Mar 4 02:10:11 UTC 2022


On Thursday, 3 March 2022 at 20:23:14 UTC, forkit wrote:
> On Thursday, 3 March 2022 at 19:28:36 UTC, matheus wrote:
>>
>> I'm a simple man who uses D with the old C mentality:
>> [...]
>> ```d
>>     string s, str = "4A0B1de!2C9~6";
>>     foreach(i;str){
>>         if(i < '0' || i > '9'){ continue; }
>>         s ~= i;
>>     }
>> ```
>>[...]
>
> mmm..but we no longer live in simple times ;-)
>
> (i.e. unicode)

If you look 
[here](https://github.com/dlang/phobos/blob/master/std/ascii.d#L315), you'll see that it's already the same logic. If it were me I would have even written like this:
```d
"4A0B1de!2C9~6".filter!(c =>
                         '0' <= c && c <= '9'
                        ).writeln; // 401296
```



More information about the Digitalmars-d-learn mailing list