How to remove all characters from a string, except the integers?
H. S. Teoh
hsteoh at quickfur.ath.cx
Thu Mar 3 21:03:40 UTC 2022
On Thu, Mar 03, 2022 at 08:23:14PM +0000, forkit via Digitalmars-d-learn 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:
> >
> > import std.stdio;
> >
> > void main(){
> > string s, str = "4A0B1de!2C9~6";
> > foreach(i;str){
> > if(i < '0' || i > '9'){ continue; }
> > s ~= i;
> > }
> > writeln("Result: ", s);
> > }
> >
> > Result: 401296
> >
> > Matheus.
>
> mmm..but we no longer live in simple times ;-)
>
> (i.e. unicode)
------
void main() {
string s = "blahblah123blehbleh456bluhbluh";
auto result = s.filter!(ch => ch.isDigit).to!int;
assert(result == 123456);
}
------
Problem solved. Why write 6 lines when 3 will do?
T
--
People tell me that I'm skeptical, but I don't believe them.
More information about the Digitalmars-d-learn
mailing list