• 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: Matthew Stuckwisch <email@hidden>
  • Date: Wed, 12 Jun 2002 15:01:21 -0500

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


set search_Text_source to "


kjhsfsf
zkhjfkz


jjklzjxc


"


set finalText to trimText(search_Text_source)

on trimText(search_Text)

--if the seacrh-Direction is 1 then we search forward, examining the
first character of the text
--for any character in the Var removeChars (in our case returns or
spaces)
--if the Seacrh-Direction is -1 then we search backward form the last
character

repeat with i in searchDirection --first search forward then backward

set search_Text_Character to character i of search_Text --the
character to examine

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
else
set search_Text to characters 2 through ((length of
search_Text) - 1) of search_Text as string
end if

--since the Search Text has been trimmed we set the search
character the NEW last or first character
set search_Text_Character to character i of search_Text
end repeat
return search_Text
end repeat

end trimText

on trimText(search_Text)
set flag to false
repeat until flag is true
set var1 to character 1 of search_Text
set var2 to character -1 of search_Text
if var2 is in {return, space} then
set search_Text to text 1 thru -2 of search_Text
if var1 is in {return, space} then set search_Text to text 2 thru -1 of search_Text
else
if var1 is in {return, space} then
set search_Text to text 2 thru -1 of search_Text
else
set flag to true
end if
end if
end repeat
return search_Text
end trimText

This one goes a little bit faster, I'll try to think of a faster one later,
of course, has probably already the absolute most optimized one already ;
-)


Matthew Stuckwisch
[AIM/MSN]{GuifaSwimmer} | [Yahoo!]{SapphireTree} | [ICQ]{137477701}
[IRC]{guifa / G}(esperNET / irc.massinova.com)
_______________________________________________
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:
    • Mac OS 9.2.2 - where did Keychain Scripting go?
      • From: JollyRoger <email@hidden>
    • Re: can I do this better?
      • From: Michelle Steiner <email@hidden>
References: 
 >can I do this better? (From: garrett <email@hidden>)

  • Prev by Date: ignoring application response on OSX
  • Next by Date: Easy, i think, Mount Volume Question?
  • Previous by thread: can I do this better?
  • Next by thread: Re: can I do this better?
  • Index(es):
    • Date
    • Thread