How to remove all characters from a string, except the integers?
BoQsc
vaidas.boqsc at gmail.com
Thu Mar 3 13:55:47 UTC 2022
On Thursday, 3 March 2022 at 13:25:32 UTC, Stanislav Blinov wrote:
> On Thursday, 3 March 2022 at 12:14:13 UTC, BoQsc wrote:
>
>> I need to check if a string contains integers,
>> and if it contains integers, remove all the regular string
>> characters.
>> I've looked around and it seems using regex is the only
>> closest solution.
>
> ```d
> import std.stdio;
> import std.algorithm : find, filter;
> import std.conv : to;
> import std.uni : isNumber;
>
> void main(string[] args){
> if (args.length > 1){
>
> auto filtered = () {
> auto r = args[1].find!isNumber; // check if a
> string contains integers
> return r.length ?
> r.filter!isNumber.to!string // and if it
> does, keep only integers
> : args[1]; // otherwise
> keep original
> } ();
>
> filtered.writeln;
>
> } else {
> write("Please write an argument.");
> }
> }
> ```
D language should be renamed into Exclamation-mark language.
It feels overused everywhere and without a better alternative.
More information about the Digitalmars-d-learn
mailing list