首页 > 编程技术 > csharp

c#中判断字符串是不是数字或字母的方法

发布时间:2020-6-25 11:40

一、判断字母
复制代码 代码如下:

string str = Console.ReadLine();
if (char.isLetter(str))
{
}
else if (char.IsDigit(str))
{
}

if(ch>='a'&&ch<='z') 小写字母
if(ch>='A'&&ch<='Z') 大写字母
数字也一样。
判断汉字一般是输入 >255 因为汉字是大字符集
二、判断输入的是不是数字
复制代码 代码如下:

try
{
int n = 0;
n = int.Parse(this.textBox1.Text.Trim());
}
catch
{
MessageBox.Show("你输入的不是数字~!");
}

也可以用Char.isNumber(str[i])一个一个字符判断。
标签:[!--infotagslink--]

您可能感兴趣的文章: