How do I use NSRange with this NSString?

前端 未结 3 417
Happy的楠姐
Happy的楠姐 2021-01-22 02:33

I have the following NSString:

productID = @\"com.sortitapps.themes.pink.book\";

At the end, \"book\" can be anything.... \"music\

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-22 02:59

    You can try a backward search for the dot and use the result to get the desired range:

    NSString *str = @"com.sortitapps.themes.pink.book";
    NSUInteger dot = [str rangeOfString:@"." options:NSBackwardsSearch].location;
    
    NSString *newStr =
       [str stringByReplacingCharactersInRange:NSMakeRange(dot+1, [str length]-dot-1)
                                    withString:@"something_else"];
    

提交回复
热议问题