set bob to "/Voulumes/MacHD/foldername"
set counter to 1
repeat with i in bob
if i contains "/" then set lastSlashPos to counter
set counter to counter + 1
end repeat
set lastString to text (lastSlashPos + 1) thru end of bob
Checked the difference between the counter method and the text item delimiter method. The text item delimiter method wins as being on average 1 millisecond faster. Big deal…:rolleyes:
Here’s the code.
set bob to "/Voulumes/MacHD/foldername"
set mgRightNow to "perl -e 'use Time::HiRes qw(time); print time'"
set mgStart to do shell script mgRightNow
set counter to 1
repeat with i in bob
if i contains "/" then set lastSlashPos to counter
set counter to counter + 1
end repeat
set lastString to text (lastSlashPos + 1) thru end of bob
set mgStop to do shell script mgRightNow
set mgRunTime to mgStop - mgStart
set ngRightNow to "perl -e 'use Time::HiRes qw(time); print time'"
set ngStart to do shell script ngRightNow
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ["/"]
set lastString to last text item of bob
set AppleScript's text item delimiters to oldDelims
set ngStop to do shell script ngRightNow
set ngRunTime to ngStop - ngStart
set timeDif to mgRunTime - ngRunTime as real
display dialog "The difference is " & timeDif & " seconds." & return & "that's " & (round (timeDif * 1000)) & " milliseconds."
I wonder since when 24 milliseconds has been considered slow…
I’d go with the cocao way too, at least when it’s about paths.
If it was words in a phrase, I’d use the componentsSeparatedByString: or componentsSeparatedByCharactersInSet: and either use as space (" ") or whitespaceCharacterSet (depending on which method is used) and then use the NSArray’s lastObject method to get the last word out of the phrase.
In AppleScript it is considered fast, Objective-C it’s considered slow and in C it’s considered extremely slow. I can do this in less than a millisecond in C on a 25 year old machine…