检查字符是否是数字?

lis*_*aro 87 javascript

我需要检查一下justPrices[i].substr(commapos+2,1).

字符串类似于:"blabla,120"

在这种情况下,它将检查'0'是否是数字.如何才能做到这一点?

Gre*_*egL 54

您可以使用比较运算符来查看它是否在数字字符范围内:

var c = justPrices[i].substr(commapos+2,1);
if (c >= '0' && c <= '9') {
    // it is a number
} else {
    // it isn't
}
Run Code Online (Sandbox Code Playgroud)


Yar*_* U. 34

你可以使用parseInt而不是检查isNaN

或者如果你想直接在你的字符串上工作,你可以像这样使用regexp:

function is_numeric(str){
    return /^\d+$/.test(str);
}
Run Code Online (Sandbox Code Playgroud)

  • @jackocnr 对于包含多个字符的字符串,您的测试还将返回 true (例如 `is_numeric_char("foo1bar") == true`)。如果你想检查数字字符`/^\d$/.test(c)`将是一个更好的解决方案。但无论如何,这不是问题:) (9认同)
  • 如果我们只需要检查单个字符,甚至更简单:`function is_numeric_char(c){return /\d/.test(c); }` (3认同)

jac*_*cnr 17

编辑:如果你只是检查一个字符(即!isNaN(parseInt(c, 10))),Blender的更新答案是正确的答案.如果你想测试整个字符串,下面的答案是一个很好的解决方案.

这是jQuery的isNumeric实现(在纯JavaScript中),它对全字符串起作用:

function isNumeric(s) {
    return !isNaN(s - parseFloat(s));
}
Run Code Online (Sandbox Code Playgroud)

该函数的注释如下:

// parseFloat NaNs数值转换误报(null | true | false |"")
// ...但误解前导数字符串,特别是十六进制文字("0x ...")
//减法强制无穷大到NaN

我想我们可以相信这些人已经花了很多时间在这上面!

评论来源在这里.超级极客在这里讨论.

  • 这是有效的,但对于仅数字检查(它适用于多位数字)是一种过度杀伤.我的解决方案可能不那么明确,但比这快得多. (2认同)

Mar*_*ian 16

我想知道为什么没有人发布过如下解决方案:

var charCodeZero = "0".charCodeAt(0);
var charCodeNine = "9".charCodeAt(0);

function isDigitCode(n) {
   return(n >= charCodeZero && n <= charCodeNine);
}
Run Code Online (Sandbox Code Playgroud)

调用如:

if (isDigitCode(justPrices[i].charCodeAt(commapos+2))) {
    ... // digit
} else {
    ... // not a digit
}
Run Code Online (Sandbox Code Playgroud)


use*_*570 15

你可以用这个:

function isDigit(n) {
    return Boolean([true, true, true, true, true, true, true, true, true, true][n]);
}
Run Code Online (Sandbox Code Playgroud)

在这里,我将其与公认的方法进行了比较:http://jsperf.com/isdigittest/5.我没想到太多,所以当我发现接受的方法慢得多时,我感到非常惊讶.

有趣的是,虽然接受的方法是更快的正确输入(例如'5')而更慢但是不正确(例如'a'),我的方法完全相反(快速为不正确,较慢为正确).

尽管如此,在最坏的情况下,我的方法比正确输入的接受解决方案快2倍,错误输入快5倍.

  • 根据这个"解决方案","长度"`(以及在数组上找到的其他属性)是数字:P (5认同)
  • 我喜欢这个答案!也许优化它:`!!([!0,!0,!0,!0,!0,!0,!0,!0,!0,!0] [n]);`它有很棒的WTF潜力和工作得很好('007'失败). (3认同)

vsy*_*ync 11

我认为找到解决这个问题的方法非常有趣.以下是一些.
(以下所有函数假设参数是单个字符.更改n[0]为强制执行)

方法1:

function isCharDigit(n){
  return !!n.trim() && n > -1;
}
Run Code Online (Sandbox Code Playgroud)

方法2:

function isCharDigit(n){
  return !!n.trim() && n*0==0;
}
Run Code Online (Sandbox Code Playgroud)

方法3:

function isCharDigit(n){
  return !!n.trim() && !!Number(n+.1); // "+.1' to make it work with "." and "0" Chars
}
Run Code Online (Sandbox Code Playgroud)

方法4:

var isCharDigit = (function(){
  var a = [1,1,1,1,1,1,1,1,1,1];
  return function(n){
    return !!a[n] // check if `a` Array has anything in index 'n'. Cast result to boolean
  }
})();
Run Code Online (Sandbox Code Playgroud)

方法5:

function isCharDigit(n){
  return !!n.trim() && !isNaN(+n);
}
Run Code Online (Sandbox Code Playgroud)

测试字符串:

var str = ' 90ABcd#?:.+', char;
for( char of str ) 
  console.log( char, isCharDigit(char) );
Run Code Online (Sandbox Code Playgroud)


Joã*_*ira 8

功能简单

function isCharNumber(c){
    return c >= '0' && c <= '9';
}
Run Code Online (Sandbox Code Playgroud)


Vla*_*nko 7

最短的解决方案是:

const isCharDigit = n => n < 10;
Run Code Online (Sandbox Code Playgroud)

您也可以应用这些:

const isCharDigit = n => Boolean(++n);

const isCharDigit = n => '/' < n && n < ':';

const isCharDigit = n => !!++n;
Run Code Online (Sandbox Code Playgroud)

如果您想检查 1 个以上的字符,您可以使用下一个变体

正则表达式:

const isDigit = n => /\d+/.test(n);
Run Code Online (Sandbox Code Playgroud)

比较:

const isDigit = n => +n == n;
Run Code Online (Sandbox Code Playgroud)

检查它是否不是 NaN

const isDigit = n => !isNaN(n);
Run Code Online (Sandbox Code Playgroud)


小智 6

我建议一个简单的正则表达式。

如果您只查找字符串中的最后一个字符:

/^.*?[0-9]$/.test("blabla,120");  // true
/^.*?[0-9]$/.test("blabla,120a"); // false
/^.*?[0-9]$/.test("120");         // true
/^.*?[0-9]$/.test(120);           // true
/^.*?[0-9]$/.test(undefined);     // false
/^.*?[0-9]$/.test(-1);            // true
/^.*?[0-9]$/.test("-1");          // true
/^.*?[0-9]$/.test(false);         // false
/^.*?[0-9]$/.test(true);          // false
Run Code Online (Sandbox Code Playgroud)

如果您只是检查单个字符作为输入,则正则表达式甚至更简单:

var char = "0";
/^[0-9]$/.test(char);             // true
Run Code Online (Sandbox Code Playgroud)


Rob*_*obG 5

如果要测试单个字符,则:

var isDigit = (function() {
    var re = /^\d$/;
    return function(c) {
        return re.test(c);
    }
}());
Run Code Online (Sandbox Code Playgroud)

将返回true或false,具体取决于c是否为数字。


Sta*_* S. 5

这是一个执行此操作的简单函数。

function is_number(char) {
    return !isNaN(parseInt(char));
}

Returns: true, false
Run Code Online (Sandbox Code Playgroud)