验证数字和中文的表达式

^[/u4e00-/u9fa5]{1,20}$ 这个是 验证中文的正则表达式

System.Text.Encoding.Default.GetByteCount( strString );--返回字节数


//判断字符串是否是数字
 string str1=this.textBox1.Text.Trim();
 if(Regex.IsMatch(str1,@"^/d+$"))
 {
  MessageBox.Show("是数字.");//是数字;
 }
 else
  MessageBox.Show("不是数字");
return;
 //

 //using System.Text.RegularExpressions;
 string str="23sd呜SDF呜95列gfwef随地56国GE嘎4";
 MatchCollection ms=Regex.Matches(str,"[/u4e00-/u9fa5]");
 string sss=null;
 for(int i=0;i<ms.Count;i++)
 {
  sss+=ms[i].Value;
 }
 MessageBox.Show(sss);

 if (Regex.IsMatch(str,"[0-9]"))//数字
  MessageBox.Show("字符串中包含有数字");
if (Regex.IsMatch(str,"[a-z]"))//小写字母
  MessageBox.Show("字符串中包含有小写字母");
 if (Regex.IsMatch(str,"[A-Z]"))//大写字母
  MessageBox.Show("字符串中包含有小写字母");
 if (Regex.IsMatch(str,"[a-zA-Z]"))//所有字母
  MessageBox.Show("字符串中包含有字母");
if (Regex.IsMatch(str,"[/u4e00-/u9fa5]"))//汉字
  MessageBox.Show("字符串中包含有汉字");


}

 


if (IsLength(TextBox.text,100) == false) //超过100字

public bool IsLength(string str, int maxLength)
{
 char c;
 int intLength = 0;

 for (int i = 0; i < str.Length; i++)
 {
  c = str[i];
 
  if ((c < 128) || ((c > 160) && (c < 224)) || ((c > 65376) && (c < 65439)))
  {
   intLength ++;
  }
  else
  {
   intLength += 2;
  }
 }

 if (intLength > maxLength)
 {
  return false;
 }

 return true;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值