02-09-2010, 02:14 AM
#1 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 19
Find occurrences of substring within a string.
Can any one help me to find out count of occurrences substring within a string.
NSString *str = @"The Quick Brown Fox Brown";
i want count of occurrence of word "Brown" from above string.
Thanks
02-09-2010, 03:02 AM
#2 (permalink )
Reena
Join Date: Apr 2009
Posts: 31
Quote:
Originally Posted by
rahul
Can any one help me to find out count of occurrences substring within a string.
NSString *str = @"The Quick Brown Fox Brown";
i want count of occurrence of word "Brown" from above string.
Thanks
NSString *str = @"The Quick Brown Fox Brown";
NSInteger count = 0;
NSArray *arr = [str componentsSeparatedByString:@" "];
for(int i=0;i<[arr count];i++)
{
if([[arr objectAtIndex:i] isEqualToString:@"Brown"]) count++; }
02-09-2010, 09:22 AM
#3 (permalink )
Registered Member
Join Date: Oct 2008
Posts: 370
is it okay ?.
NSString *str = @"The Quick Brown Fox Brown";
NSArray *arr = [str componentsSeparatedByString:@"Brown"];
NSLog(@"count = %i", [arr count]-1);
__________________
lazy blogs
The Lord's holy name be praised.
02-10-2010, 02:12 AM
#4 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 19
Finding location of subtring from string
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.
02-10-2010, 02:58 AM
#5 (permalink )
Registered Member
Join Date: Feb 2010
Location: Cincinnati, OH
Posts: 24
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;
}
02-10-2010, 05:11 AM
#6 (permalink )
Registered Member
Join Date: Jul 2009
Location: India
Posts: 78
Code:
NSString *full_string=@"The Quick Brown Fox Brown";
NSMutableArray *countloc=[[NSMutableArray alloc]init];
int temp=0;
int len=[full_string length];
for(int i =0;i<[full_string length];i++)
{
NSRange range=[full_string rangeOfString:@"Brown" options:0 range:NSMakeRange(temp,len-1)];
if(range.location<[full_string length])
[countloc addObject:[NSString stringWithFormat:@"%d",range.location]];
temp=range.location+1;
len=[full_string length]-range.location;
i=temp;
}
Here searching for the substring
Brown and
Location of the substring is stored in the array
countloc
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 587
20 members and 567 guests
Arfan , armmzz , bignoggins , BrianSlick , ckgni , dacapo , devangvyas , Duncan C , ellabayalas , Hito_kun , learningtocode , linkmx , LiquidFire , MarcoAlmeida , Monstertaco , oceanlablight , Rudy , soonw29 , Vineesh
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,774
Threads: 89,203
Posts: 380,584
Top Poster: BrianSlick (7,129)
Welcome to our newest member, ellabayalas