Re: Today's Question - poke a character in a string
Re: Today's Question - poke a character in a string
- Subject: Re: Today's Question - poke a character in a string
- From: "Arthur J. Knapp" <email@hidden>
- Date: Wed, 31 Jul 2002 13:11:16 -0400
>
From: email@hidden
>
Date: Wed, 31 Jul 2002 03:36:47 EDT
>
Subject: Today's Question - poke a character in a string
>
Is there a way to tell AppleScript to
>
>
set character 400 of MyLongString to ">"
No, vanilla AppleScript strings are not mutible in this way.
An application object, however, often is, as in:
tell app "Some Scriptable Text Editor"
tell document 1
set character 400 to "<"
>
instead of going:
>
>
set MyLongString to (characters 1 thru (CurrentIndex-1) of MyLongString) &
>
">" & (characters (CurrentIndex+1) thru end of MyLongString) as string
The above is the basic idea, breaking the string apart and then throwing
it back to gether again. However, there is a more efficient way to go about
it:
set s to "Hello World"
set s to "W" & text 2 thru 6 of s & "H" & text 8 thru -1 of s
--
--> "Wello Horld"
Using the "text X thru x" or "text from character X to character Y"
syntax is more efficient than using "characters X thru Y as string".
set s to "Hello World"
-- Obtain a list:
characters 1 thru 5 of s --> {"H", "e", "l", "l", "o"}
s's characters 1 thru 5 --> {"H", "e", "l", "l", "o"}
-- Obtain a string:
text 1 thru 5 of s --> "Hello"
text from character 1 to character 5 of s --> "Hello"
s's text 1 thru 5 --> "Hello"
s's text from character 1 to character 5 --> "Hello"
Now to get to the real issue:
>
I'm working with long strings (400 to 4000 characters),
>
searching for occurances of specific symbols, such as
>
double-greater-than (ASCII 200) and replacing them with
>
greater than symbols.
set s to FindAndReplace(s, ASCII character 200, ">")
on FindAndReplace(str, findSub, newSub)
set oldTids to text item delimiters
set text item delimiters to findSub
set str to str's text items
set text item delimiters to newSub
set str to "" & str
set text item delimiters to oldTids
return str
end FindAndReplace
You mention that your strings can get as high as 4000 characters.
You should be aware that there is an upper limit on how many
characters AppleScript will return as the result of a single coercion
from string to list, however, it would only effect you if your string
had more than 4050 some instances of the actual search character
itself, which is unlikely.
{ Arthur J. Knapp, of <
http://www.STELLARViSIONs.com>
a r t h u r @ s t e l l a r v i s i o n s . c o m
}
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.