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: has <email@hidden>
- Date: Wed, 31 Jul 2002 19:24:10 +0100
ThePPCGod wrote:
>
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.
>
>
Is there a way to tell AppleScript to
>
>
set character 400 of MyLongString to ">"
Nope. Sorry. AS strings are immutable.
>
instead of going:
>
>
set MyLongString to (characters 1 thru (CurrentIndex-1) of MyLongString) &
>
">" & (characters (CurrentIndex+1) thru end of MyLongString) as string
<stifled scream> Augh! Evil incarnate!!
This:
(characters x thru y of str) as string
is one of the nastiest constructs known to AppleScripters. Not only is it
hideously inefficient [it converts a string to list of characters, then
back to a string again], there is at least two different ways in which it
can go horribly, horribly wrong [1]. Avoid it like the plague. To get a
substring, you should use:
text x thru y of str
I'd suggest writing yourself a reusable function to perform this operation
- that way you don't have to think about the details in order to use it,
and you can add useful stuff like additional error trapping. [In a fair and
decent world, you could simply subclass AS's string class and add your own
string manipulation methods to that, but unless/until AS's OO facilities
improve significantly it's not worth the hassle.]
has
[1] These are:
- possible stack overflows on the string to list-of-characters coercion
[AS's fault]
- produces an incorrect result if TIDs are set to a non-empty value [your
fault]
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
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.