C# 通用方法 C#怎么判断字符是不是汉字

一、
/// <summary> /// 删除字符串中的中文 /// </summary> public static string Delete(string str) { string retValue = str; if (System.Text.RegularExpressions.Regex.IsMatch(str, @"[\u4e00-\u9fa5]")) { retValue = string.Empty; var strsStrings = str.ToCharArray(); for (int index = 0; index < strsStrings.Length; index++) { if (strsStrings[index] >= 0x4e00 && strsStrings[index] <= 0x9fa5) { continue; } retValue += strsStrings[index]; } } return retValue; }
二、  
/// <summary>
        /// 判断字符串是否是数字,只针对Int类型,double不行
        /// </summary>
        public static bool IsNumber(string s)
        {
            if (string.IsNullOrWhiteSpace(s)) return false;
            const string pattern = "^[0-9]*$";
            Regex rx = new Regex(pattern);
            return rx.IsMatch(s);
        }

/// <summary>
/// 判断字符串是否是 int或double,为int或double时返回true
///可用于界面上经纬度值判定
/// </summary> public static bool IsIntOrDouble(string strNumber) { Regex objNotNumberPattern = new Regex("[^0-9.-]"); Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*"); Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*"); const string strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$"; const string strValidIntegerPattern = "^([-]|[0-9])[0-9]*$"; Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")"); return !objNotNumberPattern.IsMatch(strNumber) && !objTwoDotPattern.IsMatch(strNumber) && !objTwoMinusPattern.IsMatch(strNumber) && objNumberPattern.IsMatch(strNumber); }

C#怎么判断字符是不是汉字

猜你喜欢

转载自www.cnblogs.com/macT/p/9401290.html