• 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: Replacing range within a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Replacing range within a string


  • Subject: Re: Replacing range within a string
  • From: "Arthur J Knapp" <email@hidden>
  • Date: Wed, 28 Feb 2001 11:15:27 -0500

> "Arthur J Knapp" wrote on Tue, 27 Feb 2001 18:32:18 -0500:

> Here is a more general method, I would appreciate the time anyone
> would take to thoroughly test it:
>
> on s_deleteXY(str, x, y)

Whoops!!!

Once again, Nigel Garvey has made me out to be a fool! ;-)

The vanilla handler that I posted to the list for deleting a
range of text was designed to use either positive or negative
indexes, but I neglected to test for when the user passes both
a negative and a positive index, ie:

>> set myString to "1234567890"

these are OK:

>> s_deleteXY(myString, 4, 6) --> "1237890"
>> s_deleteXY(myString, -4, -6) --> "1234890"

but this:

s_deleteXY(myString, 4, -6) --> "1234567890"

clearly is not. :(


Here is a new version of the handler, sent to me by Nigel:

on s_deleteXY(str, x, y)

-- x and y should be legitamate indexes of str,
-- positive or negative.
--
try
set z to x
character x of str
set z to y
character y of str
on error
set err to "s_deleteXY()" & return & return
error (err & z & " was out of bounds")
end try

(* This is the new section. In previous scripts of mine, I have
used negative-to-positive index conversion, but I thought that
in this particular handler I had avoided the nessesity for
doing this. Test, test, and test again... *)

-- convert negative indices to their positive equivalents
--
set len to length of str
if x < 0 then set x to len + x + 1
if y < 0 then set y to len + y + 1


-- x must be less than y
--
if (x > y) then set {x, y} to {y, x}


if x = 1 then
set s1 to ""
else
set s1 to text 1 thru (x - 1) of str
end if

if y = len then
set s2 to ""
else
set s2 to text (y + 1) thru len of str
end if

return s1 & s2

end s_deleteXY

set myString to "1234567890"

s_deleteXY(myString, 4, -1) --> "123"
s_deleteXY(myString, 4, -9) --> "1567890"


Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden

Hey, check out:
http://home.earthlink.net/~eagrant/


  • Prev by Date: Applescript with Excel
  • Next by Date: Re: global variable not being recognized by script object su
  • Previous by thread: Re: Replacing range within a string
  • Next by thread: Breaking AS
  • Index(es):
    • Date
    • Thread