Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 09-23-2011, 11:38 AM   #1 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default detect number of times a character is in a string.

I have a string with alot of text..
I want to detect how many times @"hello" is in the string... I know how to detect if it is or isn't but how do I detect the number of times it appears in the string?
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 09-23-2011, 01:52 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by orange gold View Post
I have a string with alot of text..
I want to detect how many times @"hello" is in the string... I know how to detect if it is or isn't but how do I detect the number of times it appears in the string?
I would suggest writing a method countOccurrencesOfString:inString:

You could write it by calling rangeOfStringptions:range: repeatedly. You'd set up an NSRange of the entire string to start with. Then if the first call found any occurrences, you'd increment a count, calculate a new NSRange that would start at the next character after the first occurrence, and keep searching while more occurrences are found.

Something like this:

Code:
- (int) countOccurrencesOfString: (NSString*) searchString 
  inString (String*) targetString;
{
  BOOL found;
  int count = 0;
  NSRange searchRange, foundRange;
  searchRange.location = 0;
  searchRange.length = [searchString length];
  foundRange = [targetString rangeOfString: searchString 
    options: NSLiteralSearch 
    range: searchRange.location];
  found = (foundRange.location != NSNotFound);
  while (found)
  {
    count++;
    searchRange.location = foundRange.location + foundRange.length;
    searchRange.length = [searchString length] - searchRange.location;
    if (searchRange.length < [searchString length])
      break;
    foundRange = [targetString rangeOfString: searchString 
      options: NSLiteralSearch 
      range: searchRange.location];
    found = (foundRange.location != NSNotFound);
  }
  return count;
}
Note that the code above is completely untested. I did not even check to see if it compiles. There may be syntax errors, logic errors, etc. Testing and debugging it is an "exercise for the reader."
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-23-2011, 01:57 PM   #3 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

Thanks! Since I posted this I have recieved code from another online coder... This is what he said, it seems to work great
Code:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\bhello\\b" options:NSRegularExpressionCaseInsensitive error:NULL];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:someString options:0 range:NSMakeRange(0, [string length])];
Thankyou for posting your code too though, it is neat to see other ways to do it... you build a whole counting engine.. very nice

Thanks again!
__________________
APPS4LIFE
search for me in the app store.

Last edited by orange gold; 09-23-2011 at 02:15 PM.
orange gold is offline   Reply With Quote
Old 09-23-2011, 02:18 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by orange gold View Post
Thanks! Since I posted this I have recieved code from another online coder... This is what he said, it seems to work great
Code:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\bhello\\b" options:NSRegularExpressionCaseInsensitive error:NULL];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:someString options:0 range:NSMakeRange(0, [string length])];
Thankyou for posting your code too though, it is neat to see other ways to do it... you build a whole counting engine.. very nice

Thanks again!
As predicted, my code had a few errors in it.

The regex-based version is more powerful. It will handle cases like searching for "the" when the string contains "their". My code would count "their" as containing "the". The regex version you posted will only match whole words.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-23-2011, 02:46 PM   #5 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

regex contains "options" (e.g. case-sensitive, alphanumeric only, etc.) I'm sure there is one for that too... I hope /: hhahah Because mine may need to involve that soon. Haha
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 09-23-2011, 03:29 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by orange gold View Post
regex contains "options" (e.g. case-sensitive, alphanumeric only, etc.) I'm sure there is one for that too... I hope /: hhahah Because mine may need to involve that soon. Haha
Both the code I posted and the regex string counting can do case sensitive/case insensitive, ignore diacritical marks, etc.

regex-based searching has lots more options however. You can set up a regular expression so a string only matches if it is a whole word. That' what the example you posted does. By enclosing your word between "/b" entries ("/bhello/b") it makes sure the word hello has a word break on either side.

You can also create regular expressions that will take characters from a list of characters. You could match a string that had an a, b, or c as the first character, a 1, 2, 3 as the second character, and an x, y, or z as the last character, for example.

All that would be a pain in the butt to do manually with code.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

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



» Advertisements
» Online Users: 390
15 members and 375 guests
13dario13, 7twenty7, eski, EvilElf, glenn_sayers, HemiMG, iOS.Lover, jarv, n00b, pbart, Pudding, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,905
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:17 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0