Identify if a string is a number

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

If I have these strings:

  1. \"abc\" = false

  2. \"123\" = true

  3. \"ab2\"

25条回答
  •  遥遥无期
    2020-11-22 00:23

    UPDATE of Kunal Noel Answer

    stringTest.All(char.IsDigit);
    // This returns true if all characters of the string are digits.
    

    But, for this case we have that empty strings will pass that test, so, you can:

    if (!string.IsNullOrEmpty(stringTest) && stringTest.All(char.IsDigit)){
       // Do your logic here
    }
    

提交回复
热议问题