我想在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”。我在考虑使用扫描仪。
提前致谢!
答案 0 :(得分: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:@"\""]);