Finding out whether a string is numeric or not

前端 未结 18 1556
一个人的身影
一个人的身影 2020-12-07 15:24

How can we check if a string is made up of numbers only. I am taking out a substring from a string and want to check if it is a numeric substring or not.

NSS         


        
相关标签:
18条回答
  • 2020-12-07 15:47

    Yet another option:

    - (BOOL)isValidNumber:(NSString*)text regex:(NSString*)regex {
        @try {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
            return [predicate evaluateWithObject:text];
        }
        @catch (NSException *exception) {
            assert(false); 
            return NO;
        }
    }
    

    Usage example:

    BOOL isValid = [self isValidNumber:@"1234" regex:@"^[0-9]+$"];
    
    0 讨论(0)
  • 2020-12-07 15:47

    For Swift 3

    var onlyDigits: CharacterSet = CharacterSet.decimalDigits.inverted
    if testString.rangeOfCharacter(from: onlyDigits) == nil {
      // String only consist digits 0-9
    }
    
    0 讨论(0)
  • 2020-12-07 15:49

    Test if a string is a number Might be Helpful

    int i = [@"12.3" rangeOfCharacterFromSet: [ [NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet] ].location;
    
    if (i == NSNotFound) {
         //is a number
    }
    
    0 讨论(0)
  • 2020-12-07 15:49

    Simple wrote the string extension:

    extension String {
        var isNumeric: Bool {
            return self.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil
        }
    }
    
    0 讨论(0)
  • 2020-12-07 15:50

    An extension of @John Calsbeek's answer, and clarification of @Jeff and @gyratory circus's comments.

    + (BOOL)doesContainDigitsOnly:(NSString *)string
    {
        NSCharacterSet *nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    
        BOOL containsDigitsOnly = [string rangeOfCharacterFromSet:nonDigits].location == NSNotFound;
    
        return containsDigitsOnly;
    }
    
    + (BOOL)doesContainNonDigitsOnly:(NSString *)string
    {
        NSCharacterSet *digits = [NSCharacterSet decimalDigitCharacterSet];
    
        BOOL containsNonDigitsOnly = [string rangeOfCharacterFromSet:digits].location == NSNotFound;
    
        return containsNonDigitsOnly;
    }
    

    The following can be added as category methods for NSString

    - (BOOL)doesContainDigitsOnly
    {
        NSCharacterSet *nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    
        BOOL containsDigitsOnly = [self rangeOfCharacterFromSet:nonDigits].location == NSNotFound;
    
        return containsDigitsOnly;
    }
    
    - (BOOL)doesContainNonDigitsOnly
    {
        NSCharacterSet *digits = [NSCharacterSet decimalDigitCharacterSet];
    
        BOOL containsNonDigitsOnly = [self rangeOfCharacterFromSet:digits].location == NSNotFound;
    
        return containsNonDigitsOnly;
    }
    
    0 讨论(0)
  • 2020-12-07 15:50

    The easiest and most reliable way is trying to cast as Double, if the result is nil - it can't be formed into a legit number.

    let strings = ["test", "123", "123.2", "-123", "123-3", "123..22", ".02"]
    let validNumbers = strings.compactMap(Double.init)
    
    print(validNumbers)
    // prints [123.0, 123.2, -123.0, 0.02]
    

    More info in the documentation: https://developer.apple.com/documentation/swift/double/2926277-init

    0 讨论(0)
提交回复
热议问题