In objective c, how can i check if a string/NSNumber is an integer or int
-
Please explain more fully what you mean. Both NSString and NSNumber can be converted to int or NSInteger, but neither is an int or NSInteger.– ChuckAug 13, 2010 at 4:00
-
Ok sorry, i need to know if a String or Int is an int/integer. See i'm dividing a number by another number, but the number a user puts may not be an int. i need to check if its an int number– DanielAug 13, 2010 at 4:02
6 Answers
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
}
-
4I thought I was the only one who did the !! hack to coerce to boolean. Nice! Oct 10, 2011 at 18:38
-
3This question is better than the approved answer because it gives you a true/false result. The approved answer does not. Apr 16, 2013 at 3:28
-
3This is better, because the accepted answer doesn't work as expected if you check a string with letters that starts with number. Example "1234asdf" will return 1234, not 0, this returns NO. And "000" will return 0 and you might think is wrong but it's not Jan 22, 2014 at 9:11
-
You can use the intValue method on NSString:
NSString *myString = @"123";
[myString intValue]; // returns (int)123
Here is the Apple documentation for it - it will return 0 if it's not a valid integer: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue
Hope that helps!
-
-
As in, you want to check and see if some user input is just a plain old string or a number? Like I said above, intValue returns 0 for when an NSString is not a valid integer. Say you're getting info from a UITextView - [[myTextView text] intValue] would return a valid int if possible, or 0 if it can't be validated as an int. Aug 13, 2010 at 4:13
-
17
-
12Be careful with this. This will only return 0 if the string doesn't begin with an integer value. Something like "123 street" will still return the integer 123. Source: developer.apple.com/library/mac/documentation/Cocoa/Reference/…– hebimeNov 5, 2013 at 23:42
-
1
I've put this in my category NSString (Util)
.
- (BOOL) isInt {
if ([self isEqualToString:@"0"])
return YES;
return (self.intValue != 0);
}
To check if a NSNumber if an integer try:
const char *t = [(NSNumber *)value objCType];
if (strcmp("i", t) == 0); // YES if integer
For NSString, if you use intValue/integerValue, there are certain cases which are not handled as it takes into consideration only the beginning the string.
For example @"bl3h" was not considered an integer but @" 3h" gave an output of 3
My solution for NSString:
Use :
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:strName];
NSCharacterSet *numSet = [NSCharacterSet decimalDigitCharacterSet];
if([numSet isSupersetOfSet: charSet])
{
// It is an integer (or it contains only digits)
}
if( [(NSString *)someString intValue] )
{ /* Contains an int fosho! */ }
if( [(NSNumber *)someNumber intValue] )
{ /* Contains an int wich is not 0.... :D */ }
You can ofcourse first determine whether its a NSString or NSNumber by using
[Object isKindOfClass:[NSString class]] etc...
– boolValue – charValue – decimalValue – doubleValue – floatValue – intValue – integerValue – longLongValue – longValue – shortValue – unsignedCharValue – unsignedIntegerValue – unsignedIntValue – unsignedLongLongValue – unsignedLongValue – unsignedShortValue
are all methods of NSNumber to get its values. NSString got a few similar ones.