senty senty - 10 months ago 126
iOS Question

Extract Last Word in String with Swift

What is the way of extracting last word in a String in Swift? So if I have "Lorem ipsum dolor sit amet", return "amet". What is the most efficient way of doing this?


I would also consider using componentsSeparatedByCharactersInSet, and using the whitespace character set:

let string = "Lorem ipsum dolor sit amet"
let stringArray = string.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())