Find multiple characters in a NSString

Discussion in 'iOS Programming' started by fstigre, Jan 5, 2013.

  1. fstigre, Jan 5, 2013
    Last edited by a moderator: Jan 5, 2013

    fstigre macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #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
     
  2. D.T. macrumors 603

    D.T.

    Joined:
    Sep 15, 2011
    Location:
    Vilano Beach, FL
    #2
    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"
     
  3. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #3
    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
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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).
     
  5. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #5
    Ok I will try to find the right method. Thanks a lot
     
  6. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #6
    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
     

Share This Page