Identify if a string is a number

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

If I have these strings:

  1. \"abc\" = false

  2. \"123\" = true

  3. \"ab2\"

25条回答
  •  情书的邮戳
    2020-11-22 00:31

    This is probably the best option in C#.

    If you want to know if the string contains a whole number (integer):

    string someString;
    // ...
    int myInt;
    bool isNumerical = int.TryParse(someString, out myInt);
    

    The TryParse method will try to convert the string to a number (integer) and if it succeeds it will return true and place the corresponding number in myInt. If it can't, it returns false.

    Solutions using the int.Parse(someString) alternative shown in other responses works, but it is much slower because throwing exceptions is very expensive. TryParse(...) was added to the C# language in version 2, and until then you didn't have a choice. Now you do: you should therefore avoid the Parse() alternative.

    If you want to accept decimal numbers, the decimal class also has a .TryParse(...) method. Replace int with decimal in the above discussion, and the same principles apply.

提交回复
热议问题