Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Obj-c

Idiom #137 Check if string contains only digits

Set the boolean b to true if the string s contains only characters in the range '0'..'9', false otherwise.

@import Foundation;
id nodigit=[[NSCharacterSet characterSetWithRange:NSMakeRange('0',10)].invertedSet copy];
BOOL b=![s rangeOfCharacterFromSet:nodigit].length;

Note in practice it would be better to use decimalDigitCharacterSet, which includes decimal digits of other scripts, too
B := (for all Char of S => Char in '0' .. '9');

New implementation...
< >
programming-idioms.org