Best implementation for an isNumber(string) method

前端 未结 19 2561
感动是毒
感动是毒 2020-12-15 20:04

In my limited experience, I\'ve been on several projects that have had some sort of string utility class with methods to determine if a given string is a number. The idea h

19条回答
  •  眼角桃花
    2020-12-15 20:29

    You could create an extension method for a string, and make the whole process look cleaner...

    public static bool IsInt(this string str)
    {
        int i;
        return int.TryParse(str, out i);
    }
    

    You could then do the following in your actual code...

    if(myString.IsInt())....
    

提交回复
热议问题