How to search for NSMutablearray content in substring of a NSString?
I created an NSMutableArray
and am sear开发者_JAVA技巧ching/matching the content of array with NSString's
substring but how to do it?
NSMutableArray *tmparray = {@"PPG" , @"AG" , @"HD" };
NSString *tmpstring = @"MAAPPG";
if ([tmparray containsObject:tmpstring] ) {
NSLog(@"String found");
}
But it's not happening because tmparray contain "PPG" not "MAAPPG" how to search for substring matching ?
You can use an NSPredicate
to do this, but it may be slow. Here's a simpler approach along the lines of what your trying:
NSMutableArray *tmparray = {@"PPG" , @"AG" , @"HD" };
NSString *tmpstring = @"MAAPPG";
NSRange *tmprange;
for(NSString *string in tmparray) {
tmprange = [tmpstring rangeOfString:string];
if (tmprange.location != NSNotFound) {
NSLog(@"String found");
break;
}
}
how about this
for(NSString *arrString in tmparray){
if([tmpstring rangeOfString:arrString].location != NSNotFound){
NSLog(@"String found");
}
}
NSArray *tmparray = [NSArray arrayWithObjects:@"PPG" , @"AG" , @"HD",nil];
NSString *tmpstring = @"MAAPPG";
NSRange textRange;
for(NSString *string in tmparray)
{
textRange =[tmpstring rangeOfString:string];
if(textRange.location != NSNotFound)
{
NSLog(@"String found ");
}
}
check this by using other substrings also...
精彩评论