Search code examples
cocoansstring

Validate NSString for Hexadecimal value


I'm working on a custom NSFormatter. I need to verify user input step by step... i thought to check by isPartialStringValid: if the string contains only permitted chars "0123456789ABCDEF". How can i verify this condition ? is there a way with NSString function to check if a string contain only some chars ?


Solution

  • Does this method work for you?

        NSString *string = @"FF";
    
        NSCharacterSet *chars = [[NSCharacterSet 
            characterSetWithCharactersInString:@"0123456789ABCDEF"] invertedSet];
    
        BOOL isValid = (NSNotFound == [string rangeOfCharacterFromSet:chars].location);