• 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: Capturing parts of text string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Capturing parts of text string
      • From: Robert Poland <email@hidden>
    • Re: Capturing parts of text string
      • From: kai <email@hidden>
References: 
 >Re: Capturing parts of text string (From: "Patrick Collins" <email@hidden>)

  • Prev by Date: Re: Capturing parts of text string
  • Next by Date: Re: Capturing parts of text string
  • Previous by thread: Re: Capturing parts of text string
  • Next by thread: Re: Capturing parts of text string
  • Index(es):
    • Date
    • Thread