Multiple Search in NSString
NSString *labelText = @"Choose a topic from the left to find answers for your questions about iPhone. For the latest downloads, manuals, and other resources for iPhone, choose an option below.";
NSLog(@"Label Text:--->%@",labelText);
So my issue is that I want to search iPhone text wherever is display in above nsstring and also get position if possible so please give me any idea or any开发者_运维百科 link to develop this functionality.
Take it here: (NSLog
will print indexes of your substring
positions in source
string)
- (void)findAllLocationsOfString:(NSString *)substring sourceString:(NSString *)source
{
NSRange searchRange = NSMakeRange(0, [source length]);
NSRange place = NSMakeRange(0, 0);
while (searchRange.location < [source length])
{
place = [source rangeOfString:substring options:NSLiteralSearch range:searchRange];
if (place.location != NSNotFound) NSLog(@"found here : %d", place.location);
searchRange.location = place.location + place.length;
searchRange.length = [source length] - searchRange.location;
}
}
精彩评论