How do I identify if a string is a number?

2018-12-31 03:33发布

If I have these strings:

  1. "abc" = false

  2. "123" = true

  3. "ab2" = false

Is there a command, like IsNumeric or something else, that can identify if a string is a valid number?

23条回答
长期被迫恋爱
2楼-- · 2018-12-31 04:11

The best flexible solution with .net built-in function called- char.IsDigit. It works with unlimited long numbers. It will only return true if each character is a numeric number. I used it lot of times with no issues and much easily cleaner solution I ever found. I made a example method.Its ready to use. In addition I added validation for null and empty input. So the method is now totally bulletproof

public static bool IsNumeric(string strNumber)
    {
        if (string.IsNullOrEmpty(strNumber))
        {
            return false;
        }
        else
        {
            int numberOfChar = strNumber.Count();
            if (numberOfChar > 0)
            {
                bool r = strNumber.All(char.IsDigit);
                return r;
            }
            else
            {
                return false;
            }
        }
    }
查看更多
永恒的永恒
3楼-- · 2018-12-31 04:11
十年一品温如言
4楼-- · 2018-12-31 04:12

If you want to know if a string is a number, you could always try parsing it:

var numberString = "123";
int number;

int.TryParse(numberString , out number);

Note that TryParse returns a bool, which you can use to check if your parsing succeeded.

查看更多
谁念西风独自凉
5楼-- · 2018-12-31 04:14

Double.TryParse

bool Double.TryParse(string s, out double result)
查看更多
忆尘夕之涩
6楼-- · 2018-12-31 04:19

If you want to check if a string is a number (I'm assuming it's a string since if it's a number, duh, you know it's one).

  • Without regex and
  • using Microsoft's code as much as possible

you could also do:

public static bool IsNumber(this string aNumber)
{
     BigInteger temp_big_int;
     var is_number = BigInteger.TryParse(aNumber, out temp_big_int);
     return is_number;
}

This will take care of the usual nasties:

  • Minus (-) or Plus (+) in the beginning
  • contains decimal character BigIntegers won't parse numbers with decimal points. (So: BigInteger.Parse("3.3") will throw an exception, and TryParse for the same will return false)
  • no funny non-digits
  • covers cases where the number is bigger than the usual use of Double.TryParse

You'll have to add a reference to System.Numerics and have using System.Numerics; on top of your class (well, the second is a bonus I guess :)

查看更多
弹指情弦暗扣
7楼-- · 2018-12-31 04:19

Use these extension methods to clearly distinguish between a check if the string is numerical and if the string only contains 0-9 digits

public static class ExtensionMethods
{
    /// <summary>
    /// Returns true if string could represent a valid number, including decimals and local culture symbols
    /// </summary>
    public static bool IsNumeric(this string s)
    {
        decimal d;
        return decimal.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, out d);
    }

    /// <summary>
    /// Returns true only if string is wholy comprised of numerical digits
    /// </summary>
    public static bool IsNumbersOnly(this string s)
    {
        if (s == null || s == string.Empty)
            return false;

        foreach (char c in s)
        {
            if (c < '0' || c > '9') // Avoid using .IsDigit or .IsNumeric as they will return true for other characters
                return false;
        }

        return true;
    }
}
查看更多
登录 后发表回答