• 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: Robert Poland <email@hidden>
  • Date: Mon, 4 Jul 2005 09:55:22 -0600

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

Not to "beat a dead horse"...

(* Strip leading spaces, double spaces and converts "space," to ","
*)
set current_name to " Las Cruces , New Mexico 85123-1234"


property searchStrings : {"   ", "  ", " ,"}
property replacementStrings : {" ", " ", ","}

repeat while (first item of current_name = space)
	set current_name to items 2 thru -1 of current_name as string
end repeat

repeat with new_item_name from 1 to (get count of items in searchStrings)
	set search_string to (item new_item_name of searchStrings)
	set replacement_string to (item new_item_name of replacementStrings)
	set AppleScript's text item delimiters to search_string
	set the text_item_list to every text item of the current_name
	set AppleScript's text item delimiters to replacement_string
	set the new_item_name to the text_item_list as string
	set AppleScript's text item delimiters to ""
end repeat

set new_item_name to new_item_name

set y to offset of "," in new_item_name
set city to text 1 through (y - 1) of new_item_name
try
	set testword to ((get word -2 of new_item_name) as integer)
	set zip to ((testword) as text) & "-" & word -1 of new_item_name
on error
	set zip to word -1 of new_item_name
end try
set z to offset of zip in new_item_name
set state to text (y + 2) through (z - 2) of new_item_name

{city, state, zip}

-->  {"Las  Cruces", " New  Mexico ", "85123-1234"}
--
Bob Poland - Fort Collins, CO
http://www.ibrb.org/
_______________________________________________
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: kai <email@hidden>
    • Re: Capturing parts of text string
      • From: Michelle Steiner <email@hidden>
References: 
 >Re: Capturing parts of text string (From: "Patrick Collins" <email@hidden>)
 >Re: Capturing parts of text string (From: Michelle Steiner <email@hidden>)

  • Prev by Date: Quark's print command is broken on Tiger!
  • 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