如何检查字符是 ASCII 数字还是句点?

时间:2021-02-06 13:13:06

标签: validation rust numbers character ascii

我想检查单个 char 是 ASCII 数字 ('1' - '9') 还是句点 ('.')。在 Rust 中做到这一点的最佳方法是什么?我知道 is_digit(10) 用于数字,但如何为数字或句点 (.) 执行此操作?

2 个答案:

答案 0 :(得分:7)

char::is_numeric 的一个小问题可能是它匹配的不仅仅是 ASCII <div id="player"></div> ... etc .. function onYouTubeIframeAPIReady() { // Layout resize let height = window.innerHeight; let width = window.document.getElementById('player').parentNode.clientWidth; height = width / 1.77; player = new YT.Player('player', { width: '100%', height: height, videoId: currentVideoId, playerVars: { 'autoplay': 0, 'loop': 0, 'mute': 0, 'controls': 0, 'enablejsapi': 1, 'playsinline': 0, 'rel': 0, 'widget_referrer': 'http://my domain ...' }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange, 'onError': onError } }); } ,例如:

0..=9

如果您只想匹配 ASCII '½'.is_numeric() == true ,则有 char::is_ascii_digit

后者是用 0..=9 宏实现的。您还可以将宏用于您的用例,例如:

matches!

答案 1 :(得分:6)

使用 is_numeric OR'd 和 '.' 等式检查:

fn is_numeric_or_period(c: char) -> bool {
    char::is_numeric(c) || c == '.'
}

注意:is_numeric 匹配所有数字 unicode 字符,对于 ASCII 字符串,其行为与 is_ascii_digit 相同,但如果您正在使用 unicode 字符串并且只想匹配 ASCII 数字字符,则 { {3}}。

相关问题