C# 篩選string 類型里面的漢字,獲取首字母字母,正則表達式Regex 常用驗證


界面效果

1.提取漢字

 private void buttonX1_Click(object sender, EventArgs e)
        {
            if (TxtYuan.Text.Trim() != "")
            {
                string Text = TxtYuan.Text.Trim();
                string TTT = "";
                Regex reg = new Regex("[\u4e00-\u9fa5]+");//正則表達式
                foreach (Match v in reg.Matches(Text))
                {
                    TTT += v.ToString();
                }
                TxtPinYin.Text = TTT;
         
            }
            

        }

  

2.轉換拼音首字母

  private void buttonX2_Click(object sender, EventArgs e)
        {
            if (TxtPinYin.Text.Trim() != "")
            {
                TxtShouZiMu.Text = GetFirstPinyin(TxtPinYin.Text.Trim());
            }
        }
        public static string GetFirstPinyin(string str)
        {
            string r = string.Empty;
            foreach (char obj in str)
            {
                try
                {
                    ChineseChar chineseChar = new ChineseChar(obj);
                    string t = chineseChar.Pinyins[0].ToString();
                    r += t.Substring(0, 1);
                }
                catch
                {
                    r += obj.ToString();
                }
            }
            return r;
        }

  正則表達式

    // 驗證文本框輸入為數字
        public static bool GetNum(string str)
        {
            return Regex.IsMatch(str, @"^[-]?\d+[.]?\d*$");
        }
        //驗證文本框輸入為整數
        public static bool validateNum(string strNum)
        {
            return Regex.IsMatch(strNum, "^[0-9]*$");
        }
        //  驗證文本框輸入為日期
        public static bool IsValidDate(string Date)
        { 
          bool bValid = Regex.IsMatch(Date, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
           return (bValid && Date.CompareTo("1753-01-01") >= 0);
        }
        //驗證文本框輸入為電子郵件

        public static bool IsValidEmail(string strIn)
        {
            return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }
        // 驗證文本框輸入為電話號碼
        public static bool validatePhone(string strPhone)
        {
            return Regex.IsMatch(strPhone, @"\d{3,4}-\d{7,8}");
        }
          //驗證文本框輸入為傳真號碼
         public static bool validateFax(string strFax)
         {
             return Regex.IsMatch(strFax, @"86-\d{2,3}-\d{7,8}");
         }
         //獲取IP的字符串 HttpContext.Current.Request.UserHostAddress
         public static bool IsIP(string ip)
         {
             return Regex.IsMatch(HttpContext.Current.Request.UserHostAddress, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
         }
         public static bool IsIPSect(string ip)
         {
             return Regex.IsMatch(HttpContext.Current.Request.UserHostAddress, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){2}((2[0-4]\d|25[0-5]|[01]?\d\d?|\*)\.)(2[0-4]\d|25[0-5]|[01]?\d\d?|\*)$");

         }
         //驗證字符串是否是yy-mm-dd字符串
         public static bool IsDateString(string str)
        {
             return Regex.IsMatch(str, @"(\d{4})-(\d{1,2})-(\d{1,2})");
        }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM