• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How best to extract digits from a string?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: How best to extract digits from a string?
      • From: Bill Cheeseman <email@hidden>
References: 
 >How best to extract digits from a string? (From: Bill Cheeseman <email@hidden>)

  • Prev by Date: Re: ASCII problem
  • Next by Date: Curious problem with Scripter
  • Previous by thread: How best to extract digits from a string?
  • Next by thread: Re: How best to extract digits from a string?
  • Index(es):
    • Date
    • Thread