问题
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"
回答1:
NSString *str = @"....";
NSRange lastComma = [str rangeOfString:@"," options:NSBackwardsSearch];
if(lastComma.location != NSNotFound) {
str = [str stringByReplacingCharactersInRange:lastComma
withString: @" and"];
}
来源:https://stackoverflow.com/questions/5614588/replace-last-comma-in-string-with-an-and-objective-c