检查字符是字母还是数字?

时间:2015-04-26 04:02:11

标签: rust

C isalphaisdigitisalnum的Rust等价物是什么?

1 个答案:

答案 0 :(得分:18)

is_alphabetic, is_digit, is_alphanumeric, is_numeric正是您要找的。

e.g。 :

fn main() {
    println!("1 is a digit {}", '1'.is_digit(10));
    println!("f is a hex digit {}", 'f'.is_digit(16));
    println!("a is alphabetic {}", 'a'.is_alphabetic());
    println!("こis alphabetic {}", 'こ'.is_alphabetic());
    println!("a is alphanumeric {}", 'a'.is_alphanumeric());
    println!("1 is alphanumeric {}", '1'.is_alphanumeric());
}

返回:

1 is a digit true
f is a hex digit true
a is alphabetic true
こis alphabetic true
a is alphanumeric true
1 is alphanumeric true

Rust standard library docs for chars中详细介绍了它们。