 |
 |
|
 |
02-09-2010, 01: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, 02: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, 08:22 AM
|
#3 (permalink)
|
|
Jijo Pulikkottil
Join Date: Oct 2008
Posts: 341
|
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, 01: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, 01: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, 04:11 AM
|
#6 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Location: India
Posts: 68
|
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: 421 |
| 45 members and 376 guests |
| amit.wizkid, andrei_c, AppStoreMod, bensj, Bertrand21, BrianSlick, cje, Cooper_1988, CunningCat, develsolutions, Dorald, eJohnny, Findus, fredidf, georgeburns, Gi-lo, graph, Guit, harrytheshark, HemiMG, james_womack, jaume, javaconvert, leeus, Max, MiniRobinho, mr.pagu, noobAppDeveloper, Nuncha, Panajev, pion, Pwsoccer, rahulskh, Rudy, sayhong, seejaneworkit, Shlice Ideas, Tambourin, TheZimm, twerner, walkman2001, wilky94, yannickd60, _nivek |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,301
Threads: 39,104
Posts: 171,438
Top Poster: smasher (2,575)
|
| Welcome to our newest member, alekssebasstrs |
|