If you have a list like apple,orange,lemon, you can change it to apple, orange and lemon in Objective-C using:
NSString *str = @"apple,orange,lemon";
NSRange lastComma = [str rangeOfString:@"," options:NSBackwardsSearch];
if(lastComma.location != NSNotFound) {
str = [str stringByReplacingCharactersInRange:lastComma
withString: @" and"];
}
Is there an efficient way to do this in Swift?
I can replace the last element of the array with 'and lemon'
by converting the string into an array using
var arr = str.components(separatedBy: ",")
But I am struggling with how to remove the last comma.
Thanks for any suggestions.