Remove Characters From String

I’ll begin with a Doh!, because I know this should be easy…but it just escapes me.

I have a variable that I need to remove the “+1” from,
e.g. “+1 (406) 555-5555” to “(406) 555-5555”

Any help greatly appreciated!

Thanks,

Carl

Take your pick …

set xxx to "+1 (406) 555-5555"

if xxx contains "+1 " then
	set yyy to characters 4 thru -1 of xxx as string
end if

set zzz to do shell script "echo " & quoted form of xxx & " | sed 's/+1 //'"

if xxx contains "+1 " then
	set {Astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "+1 "}
	set aaa to text items 2 thru -1 of xxx as string
	set AppleScript's text item delimiters to Astid
end if

return {aaa, yyy, zzz}

Hi.

This creates a list of characters and then coerces it to string using the current value of AppleScript’s text item delimiters. For efficiency and safety, the following would be preferable, as it extracts the subtext directly:

set yyy to text 4 thru -1 of xxx

Similarly .

. could be more efficiently rendered:

set aaa to text from text item 2 to -1 of xxx

Hi Nigel,

Good tips, especially the “text from”. Thanks!

Hello Nigel

Am’I dreaming ?
As far as I remember,


set xxx to "+1 (406) 555-5555"

set aaa to text from text item 2 to -1 of xxx

remove only the character “+”

To remove “+1”, it must be
set aaa to text from text item 3 to -1 of xxx
and, if we want to remove "+1 " which is what was done in the other examples, it must be :
set aaa to text from text item 4 to -1 of xxx

Yvan KOENIG (VALLAURIS, France) vendredi 27 avril 2012 19:15:41

a bit, Nigel’s line assumes text item delimiters set to "+1 "

Yes. I was referring to adayzdone’s suggestions in post #2 and had trimmed the quotes and my reply to focus on the particular lines.

Thanks

Clearly I misunderstood the contents of Nigel’s message.

Yvan KOENIG (VALLAURIS, France) vendredi 27 avril 2012 20:56:01