美文网首页
Objective-C用于判断字符串的类型

Objective-C用于判断字符串的类型

作者: Artificer | 来源:发表于2018-02-23 16:28 被阅读0次

    Objective-C用于判断字符串的类型

    #pragma mark - 判断全汉字
    + (BOOL)inputShouldChinese:(NSString *)inputString {
        if (inputString.length == 0) return NO;
        NSString *regex = @"[\u4e00-\u9fa5]+";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        return [pred evaluateWithObject:inputString];
    }
    
    #pragma mark - 判断全数字
    + (BOOL)inputShouldNumber:(NSString *)inputString {
        if (inputString.length == 0) return NO;
        NSString *regex = @"[0-9]*";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        return [pred evaluateWithObject:inputString];
    }
    
    #pragma mark - 判断小数
    + (BOOL)inputShouldDecimal:(NSString *)inputString {
        if (inputString.length == 0) return NO;
        NSString *regex = @"^[0-9]+(\\.[0-9]{1,2})?$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        return [pred evaluateWithObject:inputString];
    }
    
    #pragma mark - 判断全字母
    + (BOOL)inputShouldLetter:(NSString *)inputString {
        if (inputString.length == 0) return NO;
        NSString *regex = @"[a-zA-Z]*";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        return [pred evaluateWithObject:inputString];
    }
    
    #pragma mark - 判断字母或者数字
    + (BOOL)inputShouldLetterOrNum:(NSString *)inputString {
        if (inputString.length == 0) return NO;
        NSString *regex = @"[a-zA-Z0-9]*";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        return [pred evaluateWithObject:inputString];
    }
    

    相关文章

      网友评论

          本文标题:Objective-C用于判断字符串的类型

          本文链接:https://www.haomeiwen.com/subject/nipxxftx.html