對(duì)于輸入的字符串改艇,用正則表達(dá)式過濾是否是整數(shù)或者小數(shù)
+ (BOOL) ?isNumber:(NSString *) string {
????? NSString*regex =@"^[+-]?[0-9]+([.]{0,1}[0-9]+){0,1}$"; //整數(shù)或者小數(shù)的正則表達(dá)式
????? NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", regex];
????? BOOLisMatch = [predevaluateWithObject:string];
????? if(!isMatch)
????? {
????????????? NSLog(@"不是數(shù)字”);
????? }else
???? {
???????????? NSLog(@"是數(shù)字");
???? }
????? return? isMatch;
}
有關(guān)正則表達(dá)的語法和知識(shí)詳見此網(wǎng)頁http://codecloud.net/regular-3519.html