95

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.

NSString *newString = [myString substringWithRange:NSMakeRange(2,3)];
omz
  • 53,243
  • 5
  • 129
  • 141
Abhinav
  • 37,684
  • 43
  • 191
  • 309

19 Answers19

246

Here's one way that doesn't rely on the limited precision of attempting to parse the string as a number:

NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([newString rangeOfCharacterFromSet:notDigits].location == NSNotFound)
{
    // newString consists only of the digits 0 through 9
}

See +[NSCharacterSet decimalDigitCharacterSet] and -[NSString rangeOfCharacterFromSet:].

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • 6
    this is true for @"231.123" so: // newString consists only of the digits 0 through 9 and the `.` character – string.Empty Nov 14 '13 at 08:40
  • 6
    NSMutableCharacterSet *digitsAndDots = [NSMutableCharacterSet decimalDigitCharacterSet]; [digitsAndDots addCharactersInString:@"."]; NSCharacterSet *notDigitsNorDots = [digitsAndDots invertedSet]; //also, thanx for bringing in "invertedSet". I didn't know about its existence – codrut Nov 20 '13 at 11:25
  • 1
    Since you've got a mutable set there anyway, you could just call the 'invert' method on it rather than making a whole new set. – Ash Sep 26 '14 at 15:20
  • 5
    note: this method considers @"" to be a number; if applicable, you may need to also check for [newString lenght] > 0. please let me know if i'm wrong. – markckim Sep 29 '15 at 06:32
  • @NicolasTyler Are you sure? This is only working for whole numbers in my case. For example 85, 342 and 2134 all work fine, but 234.525 does not. – Supertecnoboff Sep 22 '16 at 21:14
  • 3
    @Supertecnoboff It seems this is something that has changed in IOS. This can be used instead to be sure: `[[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet]] ` – string.Empty Oct 02 '16 at 11:04
  • @NicolasTyler interesting but does that cover all locals though? Like don't some use a comma instead of a dot? – Supertecnoboff Oct 02 '16 at 11:47
  • @Supertecnoboff At the time when I did this 3 years ago the character set included `.` That doesnt seem to be the case anymore. I never tested if commas were included on other locals. But using `characterSetWithCharactersInString` could be used with any characters, if you are worried. – string.Empty Oct 03 '16 at 06:30
  • Why is [this page](https://developer.apple.com/reference/foundation/nscharacterset/1855954-decimaldigitcharacterset?language=objc) saying it's available from iOS 10+ if this answer in from 2011? – Iulian Onofrei Nov 08 '16 at 09:05
  • Why not just `NSCharacterSet *areDigits = [NSCharacterSet decimalDigitCharacterSet]` and then test for `!= NSNotFound`? – Jeff Jan 01 '17 at 07:14
  • @Jeff You'd encounter false positives for strings that contain numerical characters, but do not represent numerical values, such as`g2g` – gyratory circus Apr 04 '17 at 18:49
  • invertedSet What is the cost of that operation? It should be a huge NSCharacterSet. – KM Gorbunov Nov 26 '18 at 16:00
  • 2
    @KMGorbunov I don't think that NSCharacterSet is actually storing a backing set that contains every single character in the set. I suspect `invertedSet` is returning a class that wraps the set it was created form, and returns the opposite value for all queries. But I don't know for certain. – John Calsbeek Nov 28 '18 at 15:39
  • This solution will accept a variety of likely-unexpected strings: ``, ``, `१२३`, `١٢٣`, and `১২৩`, to name a few. [My answer](https://stackoverflow.com/a/53926300/1292061), among others, addresses this issue. – ravron Dec 25 '18 at 23:40
35

I'd suggest using the numberFromString: method from the NSNumberFormatter class, as if the number is not valid, it will return nil; otherwise, it will return you an NSNumber.

NSNumberFormatter *nf = [[[NSNumberFormatter alloc] init] autorelease];
BOOL isDecimal = [nf numberFromString:newString] != nil;
Regexident
  • 29,441
  • 10
  • 93
  • 100
Sam
  • 839
  • 10
  • 16
  • Are you sure it won't return a valid number e.g. for @"124sbd"? – Tommy May 22 '11 at 23:17
  • No, both NSNumberFormatter and NSScanner would return `NO` for your string. Also worth knowing: they also return `YES` for numbers padded with whitespace. Btw, I just added some actual code snippet to your answer, hope you don't mind. – Regexident May 22 '11 at 23:22
  • NSScanner should return 'YES' for that string, per the documentation, having found "a valid integer representation". Furthermore, it did exactly that in a test on iOS 4.3.2. However, NSNumberFormatter did return nil. – Tommy May 23 '11 at 10:56
  • This doesn't catch '.' which is a requirement for me. Answer from @John Calsbeek worked nicely. – Damian Apr 23 '12 at 08:43
  • What happens for a string with hundreds of characters all digits? Is there a limit to the NSNumber created? – Biniou Jan 11 '17 at 13:13
  • the best answer is the scanner (see Regexident's answer), it can work for numeric strings too (the formatter didn't work for numbers like -1.2 for me) – Thomas Jan 12 '20 at 06:56
12

Validate by regular expression, by pattern "^[0-9]+$", with following method -validateString:withPattern:.

[self validateString:"12345" withPattern:"^[0-9]+$"];
  1. If "123.123" is considered
    • With pattern "^[0-9]+(.{1}[0-9]+)?$"
  2. If exactly 4 digit numbers, without ".".
    • With pattern "^[0-9]{4}$".
  3. If digit numbers without ".", and the length is between 2 ~ 5.
    • With pattern "^[0-9]{2,5}$".
  4. With minus sign: "^-?\d+$"

The regular expression can be checked in the online web site.

The helper function is as following.

// Validate the input string with the given pattern and
// return the result as a boolean
- (BOOL)validateString:(NSString *)string withPattern:(NSString *)pattern
{
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

    NSAssert(regex, @"Unable to create regular expression");

    NSRange textRange = NSMakeRange(0, string.length);
    NSRange matchRange = [regex rangeOfFirstMatchInString:string options:NSMatchingReportProgress range:textRange];

    BOOL didValidate = NO;

    // Did we find a matching range
    if (matchRange.location != NSNotFound)
        didValidate = YES;

    return didValidate;
}

Swift 3 version:

Test in playground.

import UIKit
import Foundation

func validate(_ str: String, pattern: String) -> Bool {
    if let range = str.range(of: pattern, options: .regularExpression) {
        let result = str.substring(with: range)
        print(result)
        return true
    }
    return false
}

let a = validate("123", pattern: "^-?[0-9]+")
print(a)
Thomas
  • 8,306
  • 8
  • 53
  • 92
AechoLiu
  • 17,522
  • 9
  • 100
  • 118
  • I tried like this for Swift 3. ```func numberOnly(string: String) -> Int { let expression = "" let regex = NSRegularExpression.init(pattern: expression, options: .caseInsensitive) let numberOfMatches = regex.numberOfMatches(in: string, options: .reportProgress, range: NSRange.init(location: 0, length: string.characters.count)) if numberOfMatches == 0 { return Int(string)! } return 0 }``` And I got error `Playground execution aborted: error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).` – Mathi Arasan Aug 04 '17 at 06:27
  • I don't know that correct one for swift 3. Correct me if I wrong. – Mathi Arasan Aug 04 '17 at 06:49
  • How about just `return matchRange.location != NSNotFound;` – LimeRed Apr 26 '19 at 13:07
  • what about the minus sign? – Thomas Jan 12 '20 at 06:26
  • @Thomas With `^-?\d+$`, I verified it on the site: https://regex101.com – AechoLiu Jan 13 '20 at 01:14
9

You could create an NSScanner and simply scan the string:

NSDecimal decimalValue;
NSScanner *sc = [NSScanner scannerWithString:newString];
[sc scanDecimal:&decimalValue];
BOOL isDecimal = [sc isAtEnd];

Check out NSScanner's documentation for more methods to choose from.

Regexident
  • 29,441
  • 10
  • 93
  • 100
7

Swift 3 solution if need to verify that the string has only digits:

CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: myString))
plamkata__
  • 729
  • 5
  • 13
7

I think the easiest way to check that every character within a given string is numeric is probably:

NSString *trimmedString = [newString stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];

if([trimmedString length])
{
    NSLog(@"some characters outside of the decimal character set found");
}
else
{
    NSLog(@"all characters were in the decimal character set");
}

Use one of the other NSCharacterSet factory methods if you want complete control over acceptable characters.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • I like this way. Although it doesn't seem very clean, it's a very easy and core foundation-friendly way of checking if there are "non-digits" in a NSString. + I think that it's much faster than any other robot-based ways ( although we're probably not looking much at performance here ). – nembleton Jul 31 '12 at 10:49
  • I believe that stringByTrimmingCharactersInSet only trims the begin and end of the String – Brody Robertson Jun 25 '15 at 20:27
  • @BrodyRobertson: it does. Which matters not in the slightest for this answer. What example do you think this test would fail for? – Tommy Jun 26 '15 at 01:47
  • @Tommy, After reevaluating I understand your answer and it is valid and will upvote but you need you to edit to allow me to revote – Brody Robertson Jun 26 '15 at 13:33
6

This original question was about Objective-C, but it was also posted years before Swift was announced. So, if you're coming here from Google and are looking for a solution that uses Swift, here you go:

let testString = "12345"
let badCharacters = NSCharacterSet.decimalDigitCharacterSet().invertedSet

if testString.rangeOfCharacterFromSet(badCharacters) == nil {
    print("Test string was a number")
} else {
    print("Test string contained non-digit characters.")
}
TwoStraws
  • 12,862
  • 3
  • 57
  • 71
4

to be clear, this functions for integers in strings.

heres a little helper category based off of John's answer above:

in .h file

@interface NSString (NumberChecking)

+(bool)isNumber:(NSString *)string;

@end

in .m file

#import "NSString+NumberChecking.h"

@implementation NSString (NumberChecking)

+(bool)isNumber {
    if([self rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location == NSNotFound) {
        return YES;
    }else {
        return NO;
    }
}

@end

usage:

#import "NSString+NumberChecking.h"

if([someString isNumber]) {
    NSLog(@"is a number");
}else {
    NSLog(@"not a number");
}
MrTristan
  • 739
  • 5
  • 17
4

Swift 3 solution could be like:

extension String {

    var doubleValue:Double? {
        return NumberFormatter().number(from:self)?.doubleValue
    }

    var integerValue:Int? {
        return NumberFormatter().number(from:self)?.intValue
    }

    var isNumber:Bool {
        get {
            let badCharacters = NSCharacterSet.decimalDigits.inverted
            return (self.rangeOfCharacter(from: badCharacters) == nil)
        }
    }
}
hhamm
  • 1,511
  • 15
  • 22
2

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;
}
Abdurrahman Mubeen Ali
  • 1,331
  • 1
  • 13
  • 19
2

John Calsbeek's answer is nearly correct but omits some Unicode edge cases.

Per the documentation for decimalDigitCharacterSet, that set includes all characters categorized by Unicode as Nd. Thus their answer will accept, among others:

  • (U+0967 DEVANAGARI DIGIT ONE)
  • (U+1811 MONGOLIAN DIGIT ONE)
  • (U+1D7D9 MATHEMATICAL DOUBLE-STRUCK DIGIT ONE)

While in some sense this is correct — each character in Nd does map to a decimal digit — it's almost certainly not what the asker expected. As of this writing there are 610 code points categorized as Nd, only ten of which are the expected characters 0 (U+0030) through 9 (U+0039).

To fix the issue, simply specify exactly those characters that are acceptable:

NSCharacterSet* notDigits = 
    [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
if ([newString rangeOfCharacterFromSet:notDigits].location == NSNotFound)
{
    // newString consists only of the digits 0 through 9
}
ravron
  • 11,014
  • 2
  • 39
  • 66
1

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]+$"];
LuisCien
  • 6,362
  • 4
  • 34
  • 42
0

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
}
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
Ashok Kumar
  • 225
  • 4
  • 9
0

Swift extension :

extension NSString {
func isNumString() -> Bool {
    let numbers = NSCharacterSet(charactersInString: "0123456789.").invertedSet
    let range = self.rangeOfCharacterFromSet(numbers).location
    if range == NSNotFound {
        return true
    }
    return false
}  }
0

For Swift 3

var onlyDigits: CharacterSet = CharacterSet.decimalDigits.inverted
if testString.rangeOfCharacter(from: onlyDigits) == nil {
  // String only consist digits 0-9
}
Mert Celik
  • 665
  • 8
  • 12
0

When you have digits that are from mixed languages, that use (or don't) the 0-9 digits formats, you will need to run a regex that will look for any number, next thing is to convert all digits to be 0-9 format (if you need the actual value):

// Will look for any language digits
let regex = try NSRegularExpression(pattern: "[^[:digit:]]", options: .caseInsensitive)
let digitsString = regex.stringByReplacingMatches(in: string,
                                                         options: NSRegularExpression.MatchingOptions(rawValue: 0),
                                                         range: NSMakeRange(0, string.count), withTemplate: "")
// Converting the digits to be 0-9 format
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale(identifier: "EN")
let finalValue = numberFormatter.number(from: digitsString)

if let finalValue = finalValue {
  let actualValue = finalValue.doubleValue
}
OhadM
  • 4,687
  • 1
  • 47
  • 57
0

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

Ariel
  • 143
  • 10
0

Simple wrote the string extension:

extension String {
    var isNumeric: Bool {
        return self.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil
    }
}
Umair Ali
  • 758
  • 8
  • 17
0

Objective-C solution:

[NSCharacterSet.decimalDigitCharacterSet isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:field1]]

Andrew Kingdom
  • 188
  • 1
  • 9