Search code examples
objective-cnsmutablestring

Check to see if NSMutableString does not contain a specific char


Is there a way to detect if an NSString does not contain a specific char (in this case it's a "-")? For example, if I have the NSString @"-OU" and the NSString @"YOU" is there a way to fire a UIAlert when the string is @"YOU" and not @"-OU"?

EDIT: By the way I'm trying to make this dynamic for any string. I currently have the following code and want to know if this can work:

  - (BOOL) isDone:(NSString *)str{

       unichar dash = '-';

    for(int i = 0; i < [str length]; i ++){
        if([str characterAtIndex:i] != dash){
            return YES;
        }

        else{
            return NO;
        }
    }

}

This code is currently throwing the following warning in xcode: "Control may reach end of non-void function".


Solution

  • if your trying to test if the dash is in the word at all using your own build function i would do it this way

     - (BOOL) isDone:(NSString *)str{
    
           unichar dash = '-';
    
        for(int i = 0; i < [str length]; i ++){
            if([str characterAtIndex:i] == dash){
                return YES;
            }
    
        }
        return NO;
    
    }
    

    This way it will loop through entire string until it finds the dash, and will terminate as soon as it finds the dash, otherwise it will return NO once its done checking entire string.