Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2010, 01:14 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 19
Default 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
rahul is offline   Reply With Quote
Old 02-09-2010, 02:02 AM   #2 (permalink)
Reena
 
Join Date: Apr 2009
Posts: 31
Default

Quote:
Originally Posted by rahul View Post
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++;
}
reena is offline   Reply With Quote
Old 02-09-2010, 08:22 AM   #3 (permalink)
Jijo Pulikkottil
 
david_david's Avatar
 
Join Date: Oct 2008
Posts: 341
Default

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.
david_david is offline   Reply With Quote
Old 02-10-2010, 01:12 AM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 19
Default 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.
rahul is offline   Reply With Quote
Old 02-10-2010, 01:58 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Cincinnati, OH
Posts: 24
Default

Quote:
Originally Posted by rahul View Post
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;
}
bitwes is offline   Reply With Quote
Old 02-10-2010, 04:11 AM   #6 (permalink)
Registered Member
 
sumanth's Avatar
 
Join Date: Jul 2009
Location: India
Posts: 68
Default

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
sumanth is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, iphone sdk, xcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,301
Threads: 39,104
Posts: 171,438
Top Poster: smasher (2,575)
Welcome to our newest member, alekssebasstrs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:25 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0