senty senty - 1 year ago 264
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())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download