使用 NSScanner 确定字符串是否为数字 [英] determine if a string is a number with NSScanner

查看:52
本文介绍了使用 NSScanner 确定字符串是否为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定我的字符串是否包含任何 floatValue,如果是这种情况,请辞去第一响应者的职务,如果不是,则文本字段键盘应保留在屏幕上.

i'm trying to find out if my string contains any floatValue, and resign the first responder if it's the case, if it's not, the textfield keyboard should stay on screen.

这段代码总是隐藏键盘,即使它不是一个 floatValue :你知道如何让它工作吗?

This code always hides the keyboard, even if it's not a floatValue : do you know how to make it work?

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    NSScanner *scan = [NSScanner scannerWithString:[textField text]];
    if ([scan scanFloat:NULL]){

        [password resignFirstResponder];
        [passwordLength resignFirstResponder];
        return YES;

    } else {
        return NO;
    }
}

此外,我还没有尝试过循环,但这是一个开始,如果您有任何想法:

Also, i haven't tried with loops but this is a beginning, if you have any idea :

BOOL doesStringContain(NSString* string, NSString* string2){
    for (int i=0; i<[string length]; i++) {
        NSString* chr = [string substringWithRange:NSMakeRange(i, 1)];
        for (int j=0; j<[string2 length]; j++){
            if([chr isEqualToString:j])
                return TRUE;
        }
    }
    return FALSE;
}

非常感谢

推荐答案

NSScanner 将期望您的浮点数位于字符串的开头.所以为了解决这个问题,我们使用 setCharactersToBeStripped.

NSScanner will expect that your float be at the beginning of your string. So to combat that we use setCharactersToBeStripped.

setCharactersToBeStripped 将从字符串中过滤掉所有非数字非句点字符,以便您扫描的只是您要查找的数字.

setCharactersToBeStripped will filter out all non-numeric non-period characters from the string so that all you're left with to scan is the number that you're looking for.

NSScanner 将匹配 int 值(不带 .),并且将 int 123 等同于 123.00000.

NSScanner will match int values (without the .) as well as it will equate an int 123 to 123.00000.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {    
    NSScanner *scan = [NSScanner scannerWithString:[textField text]];
    [scan setCharactersToBeSkipped:[[NSCharacterSet characterSetWithCharactersInString:@"1234567890."] invertedSet]];
    float f;
    if ([scan scanFloat:&f]){
        NSLog(@"Scanned a float %f",f);
        [textField resignFirstResponder];
        return YES;

    } else {
        NSLog(@"Did not scan a float");
        return NO;
    }
}

如果您想检查是否有非数字字符与仅数字字符,请尝试:

If you want to check if there are non-numeric characters vs only numerics then try :

- (BOOL)textFieldShouldReturn:(UITextField *)textField {    

    NSCharacterSet *withFloats = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];

    NSCharacterSet *withoutFloats = [NSCharacterSet decimalDigitCharacterSet];

    // Change withFloats <-> withoutFloats depending on your need
    NSString *newString = [[textField.text componentsSeparatedByCharactersInSet:withFloats] componentsJoinedByString:@""];

    NSLog(@"newString %@", newString);

    if ([newString isEqualToString:@""]){
        NSLog(@"Scanned a numeric");
        [textField resignFirstResponder];
        return YES;

    } else {
        NSLog(@"Did not scan a numeric");
        return NO;
    }
}

这篇关于使用 NSScanner 确定字符串是否为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆