C#判断一个字符串是否是数字或者含有某个数字

第一种就是 最常见的 用Try..Catch..

再try中强转你要确认的string 类型

成功就是int  catch 就不是 

            string a = "avdfd";
            try
            {
                int b = int.Parse(a);
            }
            catch (Exception)
            {
                Console.WriteLine("不是");
            }

还有就是简单一点的

int.Tryparse()

比如 
bool  IsNumber = int.TryParse("你要判断的值",out int a);
这个更简单 但是上面那种好理解

然后就是用正则去匹配

Regex.IsMatch(input, @"^\d+$")

猜你喜欢

转载自www.cnblogs.com/CurtilageBoy/p/9228331.html
今日推荐