NSString字符位置 [英] NSString character position
本文介绍了NSString字符位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
NSString *url = @"http://stackoverflow.com/questions/ask";
如何获得第4个/的字符位置?
How can I get the character position of the 4th "/" ?
推荐答案
如果您只是想获取网址的最后一部分,您应该能够使用它:
If you're just trying to get the last part of the url, you should be able to use this:
NSArray* items = [url componentsSeparatedByString:@"/"];
获取最后一个'/'字符的索引:
To get the index of the last '/' character:
NSRange range = [url rangeOfString:@"/" options:NSBackwardsSearch];
从获取索引值range.location
要从网址中找到第四个'/'字符的索引:
To find the index of the fourth '/' character from the url:
int count = 0;
int index = -1;
for (unsigned int i=0; i < [url length]; ++i) {
if ([url characterAtIndex:i] == '/') {
++count;
if (count == 4) {
index = i;
break;
}
}
}
这篇关于NSString字符位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文