我在Objective-C中寻找一种好方法,用单词“and”替换字符串中的最后一个逗号。有什么建议吗?
“红色,绿色,蓝色,黄色”
变为
“红色,绿色,蓝色和黄色”
答案 0 :(得分:63)
NSString *str = @"....";
NSRange lastComma = [str rangeOfString:@"," options:NSBackwardsSearch];
if(lastComma.location != NSNotFound) {
str = [str stringByReplacingCharactersInRange:lastComma
withString: @" and"];
}