A regular expression can quickly determine whether a given string is a specified format. Here, some common methods are encapsulated into a string tool class.
1 Public Static classStringtool2 {3 /// <summary>4 ///determine if a string is a pure number5 /// </summary>6 /// <param name= "str" ></param>7 /// <returns></returns>8 Public Static BOOLIsnumber (stringstr)9 {Ten returnRegex.IsMatch (str,@"^[0-9]+$"); One } A - /// <summary> - ///determine if a string is a pure letter the /// </summary> - /// <param name= "str" ></param> - /// <returns></returns> - Public Static BOOLIsletter (stringstr) + { - returnRegex.IsMatch (str,@"^[a-za-z]+$"); + } A at /// <summary> - ///determine whether a string is a combination of letters or numbers - /// </summary> - /// <param name= "str" ></param> - /// <returns></returns> - Public Static BOOLIsletterornumber (stringstr) in { - returnRegex.IsMatch (str,@"(? i) ^[0-9a-z]+$"); to } + - /// <summary> the ///number of Chinese characters in the statistics string * /// </summary> $ /// <param name= "str" ></param>Panax Notoginseng /// <returns></returns> - Public Static intCountchinese (stringstr) the { + returnStr. Count (c = Regex.IsMatch (c.tostring),@"^[\u4e00-\u9fa5]{0,}$")); A } the + /// <summary> - ///determine if a string is plain Chinese $ /// </summary> $ /// <param name= "str" ></param> - /// <returns></returns> - Public Static BOOLIschinese (stringstr) the { - returnRegex.IsMatch (str,@"^[\u4e00-\u9fa5],{0,}$");Wuyi } the - /// <summary> Wu ///determines whether the string contains Chinese - /// </summary> About /// <param name= "str" ></param> $ /// <returns></returns> - Public Static BOOLHaschinese (stringstr) - { - returnRegex.IsMatch (str,@"[\u4e00-\u9fa5]"); A } + the /// <summary> - ///number of full-width characters in the statistics string $ /// </summary> the /// <param name= "str" ></param> the /// <returns></returns> the Public Static intCountsbccase (stringstr) the { - returnEncoding.Default.GetByteCount (str)-Str. Length; in } the the /// <summary> About ///determines whether the string contains full-width characters the /// </summary> the /// <param name= "str" ></param> the /// <returns></returns> + Public Static BOOLHassbccase (stringstr) - { the returnCountsbccase (str) >0;Bayi } the the /// <summary> - ///number of half-width characters in the statistics string - /// </summary> the /// <param name= "str" ></param> the /// <returns></returns> the Public Static intCountdbccase (stringstr) the { - returnStr. Length-countsbccase (str); the } the the /// <summary>94 ///determines whether the string contains half-width characters the /// </summary> the /// <param name= "str" ></param> the /// <returns></returns>98 Public Static BOOLHasdbccase (stringstr) About { - returnCountdbccase (str) >0;101 }102 103 /// <summary>104 ///determine if a string is compliant with the mailbox format the /// </summary>106 /// <param name= "str" ></param>107 /// <returns></returns>108 Public Static BOOLIsemail (stringstr)109 { the returnRegex.IsMatch (str,@"^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$");111 } the}
C # Regular Expression match string