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

forkit forkit at gmail.com
Fri Mar 4 07:55:18 UTC 2022


On Friday, 4 March 2022 at 02:10:11 UTC, Salih Dincer wrote:
> 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
> ```

If you get this question at an interview, please remember to 
first ask whether it's ascii or unicode ;-)

" All of the functions in std.ascii accept Unicode characters but 
effectively ignore them if they're not ASCII." - 
https://github.com/dlang/phobos/blob/master/std/ascii.d



More information about the Digitalmars-d-learn mailing list