Re: Replacing range within a string
Re: Replacing range within a string
- Subject: Re: Replacing range within a string
- From: "Arthur J Knapp" <email@hidden>
- Date: Tue, 27 Feb 2001 18:32:18 -0500
>
Date: Tue, 27 Feb 2001 12:23:40 -0600
>
Subject: Replacing range within a string
>
From: Bryan Harris <email@hidden>
>
... Is there an easy way to do this?
>
>
(something like: set text -6 thru -4 of aTimeString to "")
Sort of the opposite, actually:
set a to text 1 thru -7 of aTimeString
set b to text -3 thru -1 of aTimeString
set aTimeString to a & b
Note: I don't know what aTimeString is, specifically. This is just a
general way to extract what you need, based on the fact that
you don't want -6 thru -4.
Here is a more general method, I would appreciate the time anyone
would take to thoroughly test it:
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
-- x must be less than y
--
if (x > y) then set {x, y} to {y, x}
set len to length of str
if (x = 1) or (x = -len) then
set s1 to ""
else
set s1 to text 1 thru (x - 1) of str
end if
if (y = len) or (y = -1) then
set s2 to ""
else
set s2 to text (y + 1) thru -1 of str
end if
return s1 & s2
end s_deleteXY
set myString to "1234567890"
s_deleteXY(myString, 4, 6) --> "1237890"
s_deleteXY(myString, 6, 4) --> "1237890"
s_deleteXY(myString, -4, -6) --> "1234890"
s_deleteXY(myString, -6, -4) --> "1234890"
s_deleteXY(myString, 1, 1) --> "234567890"
s_deleteXY(myString, -1, -1) --> "123456789"
Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden
Hey, check out:
http://www.AppleScriptSourceBook.com