C#code,检查一个字符串是否是数字或不 [英] c# code to check whether a string is numeric or not

查看:112
本文介绍了C#code,检查一个字符串是否是数字或不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用Visual Studio 2010.I要检查一个字符串是否是数字或not.Is有任何内置的功能来检查这个还是我们需要编写一个自定义的code?

I am using Visual Studio 2010.I want to check whether a string is numeric or not.Is there any built in function to check this or do we need to write a custom code?

推荐答案

您可以使用的 int.TryParse 方法。例如:

You could use the int.TryParse method. Example:

string s = ...
int result;
if (int.TryParse(s, out result))
{
    // The string was a valid integer => use result here
}
else
{
    // invalid integer
}

有也 float.TryParse ,的double.TryParse 和的 decimal.TryParse 方法其它数值类型不是整数。

There are also the float.TryParse, double.TryParse and decimal.TryParse methods for other numeric types than integers.

但是,如果这是为了进行验证,你也可以考虑使用内置的验证在ASP控制。 NET 。下面是一个例如

But if this is for validation purposes you might also consider using the built-in Validation controls in ASP.NET. Here's an example.

这篇关于C#code,检查一个字符串是否是数字或不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆