Re: Capturing parts of text string
Re: Capturing parts of text string
- Subject: Re: Capturing parts of text string
- From: kai <email@hidden>
- Date: Mon, 4 Jul 2005 04:49:39 +0100
On Monday, July 4, 2005, at 03:45 am, Patrick Collins wrote:
Just curious if I have a string:
set x to "Las Vegas, Nevada 89102"
how is the best way to extract the city, state, and zip into their own
strings?
is the answer delimiters, or to use offsets of ", " and " "?
You could go for either, Patrick. (Offset is slightly briefer, and TIDs
can be faster on long strings - although that doesn't appear to be a
huge issue here.) Rather than locate spaces, I'd probably just use the
string elements 'word' and 'text' to help clean up the whole thing:
--------------------
on locationAsList(l)
set o to (offset of "," in l)
tell l to {text 1 thru (o - 1)} & (text o thru -1)'s {text from word 1
to word -2, word -1}
end locationAsList
set x to "Las Vegas, Nevada 89102"
set {city, state, zip} to locationAsList(x)
--------------------
on locationAsList(l)
set d to text item delimiters
set text item delimiters to ","
tell l's text items to set l to {item 1} & item 2's {text from word 1
to word -2, word -1}
set text item delimiters to d
l
end locationAsList
set x to "Las Vegas, Nevada 89102"
set {city, state, zip} to locationAsList(x)
--------------------
-- results (from either method):
city --> "Las Vegas"
state --> "Nevada"
zip --> "89102"
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden