C# 通用方法

时间:2023-03-08 22:56:12
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();
                ; 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#怎么判断字符是不是汉字

三、数字前补0

int number = 001;
var aa = "";
if (number > 1000)
{
aa = number.ToString();
}
else
{
aa = number.ToString().PadLeft(4, '0'); // 一共4位,位数不够时从左边开始用0补
}

四、Stream 和 byte[] 的相关转换