我想检查一个字符串是ios中的数字还是字母。例如“1”或“错误”谁能告诉我怎么做
谢谢
您可以使用 NSScanner:
NSScanner *scan = [NSScanner scannerWithString:yourString];
if (![scan scanFloat:NULL] || ![scan isAtEnd])
{
NSlog (@"%@", @"Is a string");
}
else
{
NSlog (@"%@", @"Is a number");
}
BOOL valid;
NSCharacterSet *alphaNums = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *inStringSet = [NSCharacterSet characterSetWithCharactersInString:String];
valid = [alphaNums isSupersetOfSet:inStringSet];
if (!valid) // Not numeric