Quote:
Originally Posted by rahul
Thanks for reply,
Can i get location of each occurrences of searched word from text
I can get searched word location but it gives me location of first occurrence of searched word within text. I want locations of all occurrences of searched word.
|
I wrote this C style (I guess) instr cause I like "instr" and didn't want to do this twice. returns the location of searchFor in searchIn, returns -1 when not found. You can use startingAt to iterate over the string and count the number of times one string occurs in another.
Code:
int instr(NSString *searchFor, NSString *searchIn, int startingAt){
NSRange searchRange;
int retVal;
searchRange.location = startingAt;
searchRange.length = [searchIn length] - startingAt;
NSRange foundRange = [searchIn rangeOfString:searchFor options:0 range:searchRange];
if(foundRange.length > 0){
retVal = foundRange.location;
}else{
retVal = -1;
}
return retVal;
}