Identify if a string is a number

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

If I have these strings:

  1. \"abc\" = false

  2. \"123\" = true

  3. \"ab2\"

25条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 00:34

    int n;
    bool isNumeric = int.TryParse("123", out n);
    

    Update As of C# 7:

    var isNumeric = int.TryParse("123", out int n);
    

    or if you don't need the number you can discard the out parameter

    var isNumeric = int.TryParse("123", out _);
    

    The var s can be replaced by their respective types!

提交回复
热议问题