您好 我有一个这样的字符串:
Results for: 123D12
2010
2009
2008
2007
2006
YEAR: 2006 WEEK: 2 PRODIDNUM: 37911
ACCESSKEY: FA3540B52F
2005
2004
YEAR: 2004 WEEK: 22 PRODIDNUM: 46178
ACCESSKEY: 58B2509373
我想发现ACCESSKEY帮助我获得十六进制(在这种情况下是FA3540B52F和58B2509373)。
问题是,当我使用rangeOfString获取accesskey时,它只会停止到第一个!这是我的代码:
if ([strippedString rangeOfString:@"ACCESSKEY"].location != NSNotFound ) {
NSUInteger int1=[strippedString rangeOfString:@"ACCESSKEY"].location;
NSString *finssid = [strippedString substringWithRange:NSMakeRange(int1+11,10)];
NSLog(@"Output = Found it %d \n",int1);
NSLog(@"Output =%@ \n \n",finssid);
}
我在这里做错了什么?
答案 0 :(得分:3)
试试这个:
NSRange searchRange = NSMakeRange(0, [strippedString length]);
BOOL keepGoing = YES;
// Find all ssid
while (keepGoing) {
NSRange accessWord = [strippedString rangeOfString:@"ACCESSKEY" options:NSCaseInsensitiveSearch range:searchRange];
if (accessWord.location != NSNotFound) {
// since we have found the access key, we can assume somethings
// ACCESSKEY: FA3540B52F
int pos = accessWord.location + 11;
NSString *ssid = [strippedString substringWithRange:NSMakeRange(pos, pos + 10)];
NSLog(@"SSID: %@", ssid);
// reset our search up a little for our next loop around
searchRange = NSMakeRange(pos, [strippedString length] - pos);
} else {
keepGoing = NO;
}
}
`
答案 1 :(得分:1)
使用rangeOfString:options:range:
代替rangeOfString:
。保存您找到匹配的索引,然后在索引之后使用range:
参数start。通过这种方式,您可以循环播放并且之前的匹配超出了您正在查看的范围。