Identify if a string is a number

前端 未结 25 3064
無奈伤痛
無奈伤痛 2020-11-22 00:13

If I have these strings:

  1. \"abc\" = false

  2. \"123\" = true

  3. \"ab2\"

25条回答
  •  难免孤独
    2020-11-22 00:19

    In case you don't want to use int.Parse or double.Parse, you can roll your own with something like this:

    public static class Extensions
    {
        public static bool IsNumeric(this string s)
        {
            foreach (char c in s)
            {
                if (!char.IsDigit(c) && c != '.')
                {
                    return false;
                }
            }
    
            return true;
        }
    }
    

提交回复
热议问题