如何在此NSString中使用NSRange? [英] How do I use NSRange with this NSString?

查看:83
本文介绍了如何在此NSString中使用NSRange?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 NSString

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

最后,书可以是任何东西......音乐,电影 ,游戏等等。

At the end, "book" can be anything.... "music", "movies", "games", etc.

我需要找到粉红色一词之后的第三个句号,这样我就可以用其他东西替换最后一个书字。我如何使用NSRange执行此操作?基本上我需要这个:

I need to find the third period after the word pink so I can replace that last "book" word with something else. How do I do this with NSRange? Basically I need this:

partialID = @"com.sortitapps.themes.pink.";


推荐答案

您可以尝试向后搜索点并使用获得所需范围的结果:

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"];

这篇关于如何在此NSString中使用NSRange?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆