Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace last comma in string with an "and". (Objective-C)

I'm looking for a good way in Objective-C to replace the last comma in a string with the word "and". Any suggestions?

"Red, Green, Blue, Yellow"

becomes

"Red, Green, Blue and Yellow"

like image 824
Snilleblixten Avatar asked Apr 10 '11 20:04

Snilleblixten


1 Answers

NSString *str = @"....";   NSRange lastComma = [str rangeOfString:@"," options:NSBackwardsSearch];  if(lastComma.location != NSNotFound) {     str = [str stringByReplacingCharactersInRange:lastComma                                        withString: @" and"]; } 
like image 59
mvds Avatar answered Sep 19 '22 09:09

mvds