• 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: can I do this better?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: can I do this better?


  • Subject: Re: can I do this better?
  • From: "Arthur J. Knapp" <email@hidden>
  • Date: Fri, 14 Jun 2002 11:49:45 -0400

> Date: Wed, 12 Jun 2002 12:47:36 -0500
> Subject: can I do this better?
> From: garrett <email@hidden>

> I wrote this little routine to trim off any trailing or beginning spaces or
> returns. would any of u pros do it differently or better? if so, how and
> why?


> property removeChars : {return, space} --text to look for for trimming
> property searchDirection : {1, -1} --1 searches forward, -1 searches
> backwards

> on trimText(search_Text)

> repeat with i in searchDirection --first search forward then backward
>
> set search_Text_Character to character i of search_Text --the

> repeat until removeChars does not contain search_Text_Character
> if i = -1 then
> set search_Text to characters 1 through ((length of
> search_Text) - 1) of search_Text as string

A few things to point out here. First, the "characters... as string"
syntax tends to be a bit slow, since several coercions are occuring.

characters x thru y of theString --> creates a list
... as string --> back to string

The better statement is:

text x thru y of theString
-- or
text from character x to character y of theStirng

as in:

set search_Text to text 1 thru (search_Text's length - 1) of search_Text


Another thing I would do is to try to limit the number of times it is
nessesary to "munge" the string. It is better to simply use a variable
index to keep one's place in the string:

set i to str's length
repeat while (str's character i = trimChar)
set i to i - 1
end
set str to str's text 1 thru i
_______________________________________________
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.

  • Follow-Ups:
    • Re: can I do this better?
      • From: Paul Berkowitz <email@hidden>
  • Prev by Date: Re: OS X: determine if Explorer is Classic
  • Next by Date: Shell script installer? Or primer?
  • Previous by thread: Re: can I do this better?
  • Next by thread: Re: can I do this better?
  • Index(es):
    • Date
    • Thread