标签:
界面效果
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})"); }
C# 筛选string 类型里面的汉字,获取首字母字母,正则表达式Regex 常用验证
标签:
原文地址:http://www.cnblogs.com/hanke123/p/5769065.html