-4

我想检查一个字符串是ios中的数字还是字母。例如“1”或“错误”谁能告诉我怎么做

谢谢

4

2 回答 2

0

您可以使用 NSScanner:

NSScanner *scan = [NSScanner scannerWithString:yourString];

if (![scan scanFloat:NULL] || ![scan isAtEnd])
{
     NSlog (@"%@", @"Is a string");
}
else
{
   NSlog (@"%@", @"Is a number");
}
于 2013-10-03T05:05:42.147 回答
0
BOOL valid;
NSCharacterSet *alphaNums = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *inStringSet = [NSCharacterSet characterSetWithCharactersInString:String];
valid = [alphaNums isSupersetOfSet:inStringSet];    
if (!valid) // Not numeric
于 2013-10-03T06:11:55.267 回答