Check if character is number?

前端 未结 22 701
误落风尘
误落风尘 2020-12-03 00:29

I need to check whether justPrices[i].substr(commapos+2,1).

The string is something like: \"blabla,120\"

In this case it would check whether \'0

22条回答
  •  天命终不由人
    2020-12-03 00:57

    The shortest solution is:

    const isCharDigit = n => n < 10;
    

    You can apply these as well:

    const isCharDigit = n => Boolean(++n);
    
    const isCharDigit = n => '/' < n && n < ':';
    
    const isCharDigit = n => !!++n;
    

    if you want to check more than 1 chatacter, you might use next variants

    Regular Expression:

    const isDigit = n => /\d+/.test(n);
    

    Comparison:

    const isDigit = n => +n == n;
    

    Check if it is not NaN

    const isDigit = n => !isNaN(n);
    

提交回复
热议问题