In objective c, how can i check if a string/NSNumber is an integer or int
Using built-in method isdigit(), each character of string is checked. If the string character is a number, it will print that string contains int. If string contains character or alphabet, it will print that string does not contain int.
The Number. isInteger() method determines whether the passed value is an integer.
Use the str. isdigit() method to check if a user input is an integer. The isdigit() method will return True for all positive integer values and False for all non-numbers, floating-point numbers or negative numbers.
If you're trying to determine whether or not an NSString
has a numeric value or not, try using NSNumberFormatter
.
-(BOOL) stringIsNumeric:(NSString *) str {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSNumber *number = [formatter numberFromString:str];
[formatter release];
return !!number; // If the string is not numeric, number will be nil
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With