senty senty - 1 year ago 217
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?

Answer Source

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())