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

前端 未结 1 1231
眼角桃花
眼角桃花 2021-02-03 22:43

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

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-03 23:11

    NSString *str = @"....";  
    NSRange lastComma = [str rangeOfString:@"," options:NSBackwardsSearch];
    
    if(lastComma.location != NSNotFound) {
        str = [str stringByReplacingCharactersInRange:lastComma
                                           withString: @" and"];
    }
    

    0 讨论(0)
提交回复
热议问题