1

我想在 NSString 中搜索特定值。

<INPUT TYPE="HIDDEN" NAME="one" VALUE="getthisvalue">
<INPUT TYPE="HIDDEN" NAME="two" VALUE="getthisvalue">
<INPUT TYPE="HIDDEN" NAME="three" VALUE="getthisvalue">
<INPUT TYPE="HIDDEN" NAME="four" VALUE="getthisvalue">

我需要在上面的代码中获取“getthisvalue”。我正在考虑使用扫描仪。

提前致谢!

4

1 回答 1

0

使用下一个方法(注意:使用 ARC):

-(NSMutableArray*)scanInString:(NSString*)string beforeValue:(NSString*)beforeValue afterValue:(NSString*)afterValue{
    NSMutableArray *foundedResults = [NSMutableArray new];
    NSString *allString = [[NSString alloc] initWithString:string];
    BOOL isHavingValue = NO;
    do{
        NSRange beforeRange = [allString rangeOfString:beforeValue];
        if(beforeRange.location != NSNotFound){
            int beforePos = beforeRange.location + beforeRange.length;
            allString = [allString substringFromIndex:beforePos];
            NSRange afterRange = [allString rangeOfString:afterValue];
            if(afterRange.location != NSNotFound){
                isHavingValue = YES;
                int afterPos = afterRange.location;
                NSRange valueRange = NSMakeRange(0, afterPos);
                NSString *value = [allString substringWithRange:valueRange];
                if(value){
                    [foundedResults addObject:value];
                }
            }
            else{
                isHavingValue = NO;
            }
        }
        else{
            isHavingValue = NO;
        }
    }while (isHavingValue);
    return foundedResults;
}

来电:

NSString *string = @"<INPUT TYPE=\"HIDDEN\" NAME=\"one\" VALUE=\"getthisvalue\"><INPUT TYPE=\"HIDDEN\" NAME=\"two\" VALUE=\"ILYA2606\"><INPUT TYPE=\"HIDDEN\" NAME=\"three\" VALUE=\"XXX\"><INPUT TYPE=\"HIDDEN\" NAME=\"four\" VALUE=\"getthisvalue\">";
NSLog(@"results = %@", [self scanInString:string beforeValue:@"VALUE=\"" afterValue:@"\""]);
于 2013-07-30T07:09:40.360 回答