• 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: Trim space chars from start of line
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trim space chars from start of line


  • Subject: Re: Trim space chars from start of line
  • From: Deivy Petrescu <email@hidden>
  • Date: Tue, 26 Jul 2011 00:14:37 -0400

On Jul 25, 2011, at 18:27 , Zavatone, Alex wrote:

> OK.  I got it down to 2 seconds from 20.
>
> Here's how.
>
> My text is return delimited on a per line basis.
>
> I added a return to the start of text or line 1 would be skipped.  Just have to remove that return after this operation.
>
> It's the looping through all the lines that is so slow.  Since the lines may begin with a space, then every line really may start with a return and possibly spaces.  So if I do a find and replace with the string return & " " and replace that with just return, using the text item delimiters, I thought it would be faster.  And it sure is.
>
> (*2 seconds*)
>
> 		-- the new elven magic
> 		set myCountAfter to 0
> 		set myCountBefore to 1
> 		repeat while myCountBefore ≠ myCountAfter
> 			set myCountBefore to length of myText
> 			log myCountBefore
> 			set myResult to SpecialFindAndReplace(myText, return & " ", return)
> 			set myText to item 2 of myResult
> 			set myCountAfter to item 1 of myResult
> 		end repeat
>
> 		-- found this somewhere and have been using it:
> 		on SpecialFindAndReplace(TheString, tofind, toreplace)
> 			set ditd to text item delimiters
> 			set text item delimiters to tofind
> 			set textItems to text items of TheString
> 			set text item delimiters to toreplace
> 			if (class of TheString is string) then
> 				set res to textItems as string
> 			else -- if (class of TheString is Unicode text) then
> 				set res to textItems as Unicode text
> 			end if
> 			set text item delimiters to ditd
> 			-- log res
> 			return {length of res, res}
> 		end SpecialFindAndReplace
>
>
> Thanks to everyone for their help today.  Made my Monday.
>
> Cheers,



What about a one-liner?

(script)
set prg to "		on SpecialFindAndReplace(TheString, tofind, toreplace)"
set prg to text (get offset of (character 1 of word 1 of prg) in prg) thru -1 of prg
prg
(script)

That ought to beat the 2 seconds mark….



Deivy Petrescu
email@hidden



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Trim space chars from start of line
      • From: Shane Stanley <email@hidden>
References: 
 >Trim space chars from start of line (From: "Zavatone, Alex" <email@hidden>)
 >Re: Trim space chars from start of line (From: Ron Hunsinger <email@hidden>)
 >Re: Trim space chars from start of line (From: "Zavatone, Alex" <email@hidden>)

  • Prev by Date: Re: Note on ASE scripting
  • Next by Date: Re: Trim space chars from start of line
  • Previous by thread: Re: Trim space chars from start of line
  • Next by thread: Re: Trim space chars from start of line
  • Index(es):
    • Date
    • Thread