Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 27, 2009, 02:08 PM   #1
StijnSpijker
macrumors newbie
 
Join Date: Jan 2009
Location: The Netherlands
Counting character occurence in NSString

I'm just working on my very first Cocoa project. I already know a number of other languages such as C / C++ / C# / Java etc..

I'm actually trying to accomplish a simple task: Finding the number of occurences of the character '-' in an NSString. Now, I could write a 6 line while loop to do this for me, but to be honest, I expected the Cocoa framework to have a method for me?

I could only think of one alternative way: rangeOfCharacterFromSet. And then looping that until the end of the string (but this would be inferior to looping once, of course..).

Have I missed a vital search keyword on google (I could not find a -single- article about finding the number of occurences in an NSString)? I did find a some articles about having a CountedSet, but you would have to have an array? (Do I have to go back to char arrays??).

I would appreciate some hints as a first time user. Thanks!
StijnSpijker is offline   0 Reply With Quote
Old Jan 27, 2009, 02:26 PM   #2
LostSoul80
macrumors 68020
 
LostSoul80's Avatar
 
Join Date: Jan 2009
Quote:
Originally Posted by StijnSpijker View Post
I'm just working on my very first Cocoa project. I already know a number of other languages such as C / C++ / C# / Java etc..

I'm actually trying to accomplish a simple task: Finding the number of occurences of the character '-' in an NSString. Now, I could write a 6 line while loop to do this for me, but to be honest, I expected the Cocoa framework to have a method for me?

I could only think of one alternative way: rangeOfCharacterFromSet. And then looping that until the end of the string (but this would be inferior to looping once, of course..).

Have I missed a vital search keyword on google (I could not find a -single- article about finding the number of occurences in an NSString)? I did find a some articles about having a CountedSet, but you would have to have an array? (Do I have to go back to char arrays??).

I would appreciate some hints as a first time user. Thanks!
Maybe this can be a workaround (put this in your NSString subclass)

Code:
- (NSInteger)occurrencesOfCharacter:(char *)c
{
    if (c == NULL) return -1;

    NSString * s = [NSString stringWithCString: c encoding: NSUTF8StringEncoding]; // can be every encoding you want...
    if ([self isEqualToString: s]) return 1; // just to avoid useless work
    NSUInteger  cnt = [[self componentsSeparatedByString: s] count];
    return cnt - 1;
}
Hope that helps.
LostSoul80 is offline   0 Reply With Quote
Old Jan 27, 2009, 02:36 PM   #3
StijnSpijker
Thread Starter
macrumors newbie
 
Join Date: Jan 2009
Location: The Netherlands
Quote:
Originally Posted by LostSoul80 View Post
Maybe this can be a workaround (put this in your NSString subclass)

Code:
- (NSInteger)occurrencesOfCharacter:(char *)c
{
    if (c == NULL) return -1;

    NSString * s = [NSString stringWithCString: c encoding: NSUTF8StringEncoding]; // can be every encoding you want...
    if ([self isEqualToString: s]) return 1; // just to avoid useless work
    NSUInteger  cnt = [[self componentsSeparatedByString: s] count];
    return cnt - 1;
}
Hope that helps.
Thanks!
But I think that writing the while loop myself requires less lines of code, and has WAY better readability ..

I conclude from this post that there is no such method in the framework, and will write my own helper class..
StijnSpijker is offline   0 Reply With Quote
Old Jan 27, 2009, 03:06 PM   #4
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Shorter version
Code:
- (int)occurrencesOfString:(NSString *)str
{
    return [[self componentsSeparatedByString:str] count]-1;
}
I'd avoid using chars directly if working with NSStrings.
kainjow is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Weird occurence with finder being...annoying 5krapbrain iMac 3 Jan 9, 2014 12:51 PM
2 weeks and counting..and no 5s :( Krissypantz2828 iPhone 14 Oct 13, 2013 09:43 AM
GPS Satellite Counting on iphone. DuganRun iPhone and iPod touch Apps 1 Apr 1, 2013 05:02 PM

Forum Jump

All times are GMT -5. The time now is 06:23 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC