getting last word of a string

Hi everyone,

my problem is I need to seperate the last word of a string.
e.g. for “/Voulumes/MacHD/foldername” I would need “foldername” as a seperate string.

I was using text item delimiters before to replace or delete parts of strings but I am not sure how to “grab” a part of it.

Any suggestions?

Thanks in advance.

Cheers,
Chris

Quite simply:


set bob to "/Voulumes/MacHD/foldername"
set fred to word 3 of bob

However, you may want to reverse the string, find the first /, use this as an offset, then reverse the resulting string back (or something)

Feeling inspired, so:

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

Hi Woggledog,

awesome!
I was getting close to this but bissing the

text (lastSlashPos + 1) thru end of bob

part of it… :slight_smile:

Thanks again for the help!

Hi,

the Cocoa way


set bob to current application's NSString's stringWithString_("/Volumes/MacHD/foldername")
set fred to bob's lastPathComponent()

@Woggledog,
It’s the ASOC forum, so I’d go with StefanK’s answer as more efficient and relevant. wink :wink:

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."

The ASOC code is still faster :wink:

This is a neat way to get the last word. You just use −1 that denotes the last element, regardless of how many.

set bob to "/Voulumes/On/A/MacHD/with a long/foldername"
set fred to word -1 of bob

While this is the most conceptually simple, pure Applescript approach, it is also slow. It takes an average of 24 milliseconds to run.:confused:

Cheers!

I wonder since when 24 milliseconds has been considered slow… :wink:

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.

Just my two cents on the subject.

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… :smiley: