Saturday, November 1, 2014

#1 2012-04-26 01:14:29 pm

ckeyes888
Member
Registered: 2010-04-27
Posts: 125

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

Offline

 

#2 2012-04-26 02:17:49 pm

adayzdone
Member
From: New York
Registered: 2011-01-24
Posts: 480

Re: Remove Characters From String

Take your pick ...

Applescript:

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}

Last edited by adayzdone (2012-04-26 02:23:16 pm)

Offline

 

#3 2012-04-26 05:52:26 pm

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 3555

Re: Remove Characters From String

Hi.

adayzdone wrote:

Applescript:

set yyy to characters 4 thru -1 of xxx as string

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:

Applescript:

set yyy to text 4 thru -1 of xxx

Similarly …

Applescript:

set aaa to text items 2 thru -1 of xxx as string

… could be more efficiently rendered:

Applescript:

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


NG

Online

 

#4 2012-04-26 06:04:43 pm

adayzdone
Member
From: New York
Registered: 2011-01-24
Posts: 480

Re: Remove Characters From String

Hi Nigel,

Good tips, especially the "text from". Thanks!

Offline

 

#5 2012-04-27 12:16:00 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 1528

Re: Remove Characters From String

Hello Nigel

Am'I dreaming ?
As far as I remember,

Applescript:


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

Offline

 

#6 2012-04-27 12:22:34 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10624
Website

Re: Remove Characters From String

Yvan Koenig wrote:

Am'I dreaming ?

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

Last edited by StefanK (2012-04-27 12:26:14 pm)


regards

Stefan

Offline

 

#7 2012-04-27 12:57:44 pm

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 3555

Re: Remove Characters From String

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.

Last edited by Nigel Garvey (2012-04-27 03:49:11 pm)


NG

Online

 

#8 2012-04-27 01:56:08 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 1528

Re: Remove Characters From String

Thanks

Clearly I misunderstood the contents of Nigel's message.

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

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.030 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)