开发者

iPhone rangeOfString

开发者 https://www.devze.com 2023-02-19 08:46 出处:网络
Hello I have a string like this: Results for: 123D12 2010 2009 2008 2007 2006 YEAR: 2006 WEEK: 2 PRODIDNUM: 37911

Hello I have a string like this:

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

and I want to spot the ACCESSKEY to help m开发者_如何学Ce get the hexadecimal(in this case are FA3540B52F and 58B2509373).

The issue is that when I use rangeOfString to get the accesskey it stops only to the first one! This is my code:

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

What I am doing wrong here?


Try this:

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

`


Use rangeOfString:options:range: instead of rangeOfString:. Save the index you found a match at, then using the range: argument start on the index after that. This way you loop through and previous matches fall outside the range you are looking in.

0

精彩评论

暂无评论...
验证码 换一张
取 消