[REBOL] Re: Fast way to remove all non-numerical chars from a string
From: carl::cybercraft::co::nz at: 22-Sep-2007 11:46
On Friday, 21-September-2007 at 15:45:16 Kai Peters wrote,
>Hi ~
>
>what is a very fast way to remove all non-numerical characters from
>a given string? I will have to process almost a million of these, so speed
>matters.
>
>I.e., "(250) 764-0929" -> "2507640929"
I'm not sure how fast this would compare to other methods, but give it a go...
First, create a string containing all characters, less the ten numerals...
chrs: ""
repeat n 256 [append chrs to-char n - 1]
chrs: exclude chrs "1234567890"
Then trim your strings thus...
trim/with "(250) 764-0929" chrs
Hmmm. Well - it might work, depending on the type of characters in your string. It
works on your example, but not on a string made up of random characters. Can anyone
explain if that's expected behaviour? ie...
>> chrs: ""
== ""
>> repeat n 256 [append chrs to-char n - 1]
== {^-^A^B^C^D^E^F^G^H^-
^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\^]^!^_ !"#$%&'()*+,-./0123456789:;<=>?-ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^^...
>> chrs: exclude chrs "1234567890"
== {^-^A^B^C^D^E^F^G^H^-
^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\^]^!^_ !"#$%&'()*+,-./:;<=>?-ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^^_`{|}~^~...
>> str: ""
== ""
>> loop 1000 [append str to-char random 255]
== {^\^-^H^!^Ly8f?^Q垧H^_m4^ZV-^-OF^{-W^SwF^_38^H^\aV^NՌWuj)V^C^~^\%^B'...
>> trim/with "(250) 764-0929" chrs
== "2507640929"
>> trim/with str chrs
== {y8fm4w38aujft0iltp9wlnegnxsr2lub31hal3uyoczbqnbrly5yesva4...
???
-- Carl Read.