using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { char a = 'a'; char b = '8'; char c = 'L';
if (char.IsLetter(a)) { label1.Text = a + "为字母"; } else { label1.Text = a + "不为字母"; } if (char.IsDigit(b)) { label2.Text = b + "为数字"; } else { label2.Text = b + "不为数字"; } if (char.IsLetterOrDigit(c)) { label3.Text = c + "为数字或字母"; } else { label3.Text = c + "不为数字或字母"; }
static void Main(string[] args) { string[] testArray = new string[] { "全汉字", "All letter", "8888" }; Regex regExp = new Regex(@"(?isn)^((?<chinese>[\u4E00-\u9FA5]+)|(?<letter>[a-z]+)|(?<num>\d+))$", RegexOptions.Compiled); Regex regExp2 = new Regex(@"(?isn)^((?<chinese>[\u4E00-\u9FA5\s]+)|(?<letter>[a-z\s]+)|(?<num>\d+))$", RegexOptions.Compiled);//带标点符号换行支持,看你实际需要选择 foreach (string test in testArray) { Match m = regExp2.Match(test); Console.Write(test); if (m.Groups["chinese"].Success) Console.WriteLine(" 是汉字"); if (m.Groups["letter"].Success) Console.WriteLine(" 是字母"); if (m.Groups["num"].Success) Console.WriteLine(" 是数字"); } Console.ReadKey(); } 结果: 全汉字 是汉字 All letter 是字母 8888 是数字
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
char a = 'a';
char b = '8';
char c = 'L';
if (char.IsLetter(a))
{
label1.Text = a + "为字母";
}
else
{
label1.Text = a + "不为字母"; } if (char.IsDigit(b))
{
label2.Text = b + "为数字";
}
else
{
label2.Text = b + "不为数字"; } if (char.IsLetterOrDigit(c))
{
label3.Text = c + "为数字或字母";
}
else
{
label3.Text = c + "不为数字或字母"; }
}
}
}
Regex.IsMatch("",@"^[A_Za_z]");
Regex.IsMatch("",@ "^[0-9]+$ "))
static void Main(string[] args)
{
string[] testArray = new string[] { "全汉字", "All letter", "8888" };
Regex regExp = new Regex(@"(?isn)^((?<chinese>[\u4E00-\u9FA5]+)|(?<letter>[a-z]+)|(?<num>\d+))$", RegexOptions.Compiled);
Regex regExp2 = new Regex(@"(?isn)^((?<chinese>[\u4E00-\u9FA5\s]+)|(?<letter>[a-z\s]+)|(?<num>\d+))$", RegexOptions.Compiled);//带标点符号换行支持,看你实际需要选择
foreach (string test in testArray)
{
Match m = regExp2.Match(test);
Console.Write(test);
if (m.Groups["chinese"].Success) Console.WriteLine(" 是汉字");
if (m.Groups["letter"].Success) Console.WriteLine(" 是字母");
if (m.Groups["num"].Success) Console.WriteLine(" 是数字");
}
Console.ReadKey();
}
结果:
全汉字 是汉字
All letter 是字母
8888 是数字