Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fstigre

macrumors regular
Original poster
Aug 12, 2008
158
1
How can I find ALL instances of the same character in a string. For instance in the following code it finds the first character in the string, but what I want is to check if something exists multiple times, I don't really want to know how many they are only if there are multiples.

Code:
- (IBAction)findCharacters:(id)sender {
    
    NSString *number = @"Hello World";
    NSRange character = [number rangeOfString:@"l"];
    
    if (character.location == NSNotFound) {
        NSLog(@"This string DOESN'T contain that character");
    }
    else{
        NSLog(@"This string DOES contain that character");
    }    
}

Is there a way to check if a character exists more than once in a NSString?

Thanks a lot
 
Last edited by a moderator:

D.T.

macrumors G4
Sep 15, 2011
11,050
12,460
Vilano Beach, FL
Yeah, I'd probably use NSRegularExpression which has a native numberOfMatchesInString method that returns an int of the hits on the search string.

Alternately, you could loop through using the rangeOfString, and moving the starting range to the index of the found string (i.e., start 0, then search at start index of previous found string), then the recursions would be the "found count"
 

fstigre

macrumors regular
Original poster
Aug 12, 2008
158
1
Thank you for your reply!

I like your second option, in fact I tried doing this with a nested if but it didnt work. Is there a way to do a second check after finding the first one? How can I change the starting point of the rangeOfString?

Thanks
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
Is there a way to do a second check after finding the first one? How can I change the starting point of the rangeOfString?

Learning to use the reference docs is a crucial programming skill. Read the class reference doc for NSString, either online or in Xcode's reference docs. Locate the rangeOfString: method you know, and look for similar methods that take additional parameters, such as a range that defines where to search.

Also read the Companion Guide for the NSString class, called "String Programming Guide". There is a section that "describes methods for finding characters and substrings within strings and for comparing one string to another." (quoted directly from the Introduction page).
 

fstigre

macrumors regular
Original poster
Aug 12, 2008
158
1
Ok I will try to find the right method. Thanks a lot
 

fstigre

macrumors regular
Original poster
Aug 12, 2008
158
1
Here is what I did just in case someone has the same question. Again all I wanted to do is check a string and find out if there were multiple instances of one character.

Code:
- (IBAction)findCharacters:(id)sender
{
    NSString *mainNumber =  @"123.45.67";
    NSString *findThisCharacter = @".";
    
    NSRange firstInstance = [mainNumber rangeOfString:findThisCharacter];
    NSUInteger firstLocation =  firstInstance.location;
    
    if (firstLocation == NSNotFound)
    {
        NSLog(@"First check, does not exist");  
    }
    else
    {
        NSUInteger secondStartingPoint =  firstLocation + 1;
        
        NSString *remainingNumber = [mainNumber substringFromIndex:secondStartingPoint];
        NSRange secondInstance = [remainingNumber rangeOfString:findThisCharacter];
        
        NSUInteger secondLocation =  secondInstance.location;
        
        if (secondLocation == NSNotFound)
        {
            NSLog(@"Second check, does NOT exist");
        }
        else
        {
         NSLog(@"Second check found, it is at location: %i", secondLocation); 
        }
    }  
}

What I did is basically used rangeOfString to find the first instance and than used substringFromIndex to find the second instance because with substringFromIndex you can specify the point from where to start looking.

Thank you all a lot
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.