iphonerangeofstring

iphonerangeofstring,iphone,objective-c,nsstring,Iphone,Objective C,Nsstring,你好 我有这样一个字符串: 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)

你好 我有这样一个字符串:

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);    
}

我在这里做错了什么?

使用
rangeOfString:options:range:
而不是
rangeOfString:
。保存在中找到匹配项的索引,然后使用
范围:
参数在该索引上启动。通过这种方式,您可以循环浏览,以前的匹配不在您要查找的范围内。

使用
rangeOfsString:options:range:
而不是
rangeOfsString:
。保存在中找到匹配项的索引,然后使用
范围:
参数在该索引上启动。通过这种方式,您可以循环浏览,以前的匹配不在您要查找的范围内。

尝试以下方法:

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;
    }
}
`试试这个:

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;
    }
}

`

我已经读过这篇文章,但我不知道如何实现它。你能给我举个例子吗?我自己不确定确切的格式,只是在文档中查了一下。你没有弄清楚哪一部分?这一部分:如果([strippedString rangeOfString:@“ACCESSKEY”].location!=NSNotFound)实际上它在第一个ACCESSKEY上停止,我想找到字符串中的所有键。我已经读过这方面的内容,但我没有弄清楚如何实现它。你能给我举个例子吗?我自己不确定确切的格式,只是在文档中查了一下。你没有弄清楚哪一部分?这一部分:如果([strippedString rangeOfString:@“ACCESSKEY”].location!=NSNotFound)实际上它在第一个ACCESSKEY上停止,我想找到字符串中的所有键。它应该可以工作,但我收到了以下错误:由于未捕获的异常“NSRangeException”而终止应用程序,原因:'***-[NSCFString substringWithRange:]:范围或索引超出范围'好的,我会修复它!您编写了以下内容:NSString*ssid=[strippedString substringWithRange:NSMakeRange(pos,pos+10)];而不是:NSString*fssid=[strippedString substringWithRange:NSMakeRange(pos,10)];顺便说一句,非常感谢!!它应该可以工作,但我收到了以下错误:由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[NSCFString substringWithRange:]:范围或索引超出边界”好的,我修复了它!您编写了以下命令:NSString*ssid=[strippedstringsubstringwithrange:NSMakeRange(pos,pos+10)];而不是:NSString*fssid=[strippedString substringWithRange:NSMakeRange(pos,10)];顺便说一句,非常感谢!!