Re: Capturing parts of text string
Re: Capturing parts of text string
- Subject: Re: Capturing parts of text string
- From: Michelle Steiner <email@hidden>
- Date: Sun, 3 Jul 2005 22:27:18 -0700
On Jul 3, 2005, at 7:45 PM, Patrick Collins wrote:
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 " "?
Well, there is the problem that the city and state can be more than
one word. The zip code can be one or two words, depending on whether
it is the basic zip code or zip plus four, because the "-" is
considered to be a word separator. So, here is what I came up with;
it will handle multi-word city names, multi-word state names, and
either basic five-digit zip codes or "ZIP plus 4":
set x to "Las Vegas, Nevada 89102"
--set x to "Las Cruces, New Mexico 85123-1234"
set y to offset of "," in x
set city to text 1 through (y - 1) of x
try
set testword to ((get word -2 of x) as integer)
set zip to ((testword) as text) & "-" & word -1 of x
on error
set zip to word -1 of x
end try
set z to offset of zip in x
set state to text (y + 2) through (z - 2) of x
{city, state, zip}
The code, though, assumes a single space after the comma, and a
single space separating the state from the ZIP code.
-- Michele
--
Give Peace A Chance.
_______________________________________________
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