• 一些简单的验证(中文验证、数字验证)


    ^[\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;
    }

  • 相关阅读:
    圣战 [奇环, 树上差分]
    花火之声不闻于耳 [线段树]
    SP2878 KNIGHTS
    P5300 [GXOI/GZOI2019]与或和 [单调栈]
    Speike [线段树, 动态规划]
    Jerry [动态规划]
    JSON对象
    正则书写
    flex布局实践
    数组对象的深拷贝与浅拷贝
  • 原文地址:https://www.cnblogs.com/JimZhang/p/229522.html
Copyright © 2020-2023  润新知