Re: How best to extract digits from a string?
Re: How best to extract digits from a string?
- Subject: Re: How best to extract digits from a string?
- From: Paul Berkowitz <email@hidden>
- Date: Wed, 25 Apr 2001 20:46:18 -0700
On 4/25/01 2:04 PM, "Bill Cheeseman" <email@hidden> wrote:
>
So now I filter for numerical digits as in this example:
>
>
set digits to "1234567890"
>
set input to "(800) 555-1212"
>
set output to "" -- initialize
>
repeat with char in every character of input
>
if char is in digits then set output to output & char
>
end repeat
>
get output --> "8005551212"
>
>
Is there a more efficient way to do this?
For long strings, or a lot of them, this will be better, avoiding any
concatenation:
set digits to "1234567890"
set input to "++(800) 555-1212 - Mr. Jones"
set output to {} -- initialize
repeat with i from 1 to (count input)
set char to character i of input
if char is in digits then set end of output to char
end repeat
set AppleScript's text item delimiters to {""}
set output to "" & output
-- "8005551212"
Or RegEx can do a simple grep instead.
--
Paul Berkowitz