如何正确解析子串的字符串?

时间:2013-03-08 23:35:26

标签: iphone string exception range substring

我是iphone开发的菜鸟,我正在尝试解析字符串的子字符串。我解析的子字符串每次解析时的长度都会有所不同,所以我使用带有范围的子字符串来表示子字符串始终位于两个字符之间。问题是当我得到一个异常时说出异常 - - [__ NSCFString substringWithRange:]:范围或索引越界。非常感谢任何帮助。

我的代码

NSString * storyLink = @"http://link.brightcove.com/services/link/bcpid1683318714001/bclid1644543007001/bctid2212677853001?src=mrss"//<--Parsing the numbers between "bctid" & "?src"
NSRange start = [storyLink rangeOfString:@"d" options:NSBackwardsSearch];
NSRange end = [storyLink rangeOfString:@"?" options:NSBackwardsSearch];
NSString *clipid = [storyLink substringWithRange:NSMakeRange(start.location, end.location)];//<--Exception thrown here
NSLog(@"clipid: %@", clipid);

1 个答案:

答案 0 :(得分:2)

范围被解释为{ begin, length }形式。 NSRange结构的成员被称为locationlength而不是beginend是有原因的。将您的代码更改为

NSMakeRange(start.location, end.location - start.location)

它会正常工作。

相关问题