程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> C#關於類型判斷和類型轉換的通用方法,

C#關於類型判斷和類型轉換的通用方法,

編輯:關於.NET

C#關於類型判斷和類型轉換的通用方法,


     代碼編寫的過程中,較多的會涉及到基本數據類型的使用和定義,在項目中,對於類型的判定和類型間的相互轉換,有時也較為的麻煩,先提供幾種基本數據類型的判斷方法:

1.判斷對象是否為Int32類型的數字:

        /// <summary>
        /// 判斷對象是否為Int32類型的數字
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(object expression)
        {
            return expression != null && IsNumeric(expression.ToString());
        }

        /// <summary>
        /// 判斷對象是否為Int32類型的數字
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(string expression)
        {
            var str = expression;
            if (!(str.Length > 0) || str.Length > 11 || !System.Text.RegularExpressions.Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$")) return false;
            return (str.Length < 10) || (str.Length == 10 && str[0] == '1') ||
                   (str.Length == 11 && str[0] == '-' && str[1] == '1');
        }

2.是否為Double類型:

        /// <summary>
        /// 是否為Double類型
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool IsDouble(object expression)
        {
            if (expression != null)
                return System.Text.RegularExpressions.Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");

            return false;
        }

3.將字符串轉換為數組:

        /// <summary>
        /// 將字符串轉換為數組
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>字符串數組</returns>
        public static string[] GetStrArray(string str)
        {
            return str.Split(new char[',']);
        }

4.將數組轉換為字符串:

        /// <summary>
        /// 將數組轉換為字符串
        /// </summary>
        /// <param name="list">List</param>
        /// <param name="speater">分隔符</param>
        /// <returns>String</returns>
        public static string GetArrayStr(List<string> list, string speater)
        {
            var sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == list.Count - 1)
                {
                    sb.Append(list[i]);
                }
                else
                {
                    sb.Append(list[i]);
                    sb.Append(speater);
                }
            }
            return sb.ToString();
        }

5.object型轉換為bool型:

        /// <summary>
        /// object型轉換為bool型
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換後的bool類型結果</returns>
        public static bool StrToBool(object expression, bool defValue)
        {
            if (expression != null)
                return StrToBool(expression, defValue);

            return defValue;
        }

6.string型轉換為bool型:

        /// <summary>
        /// string型轉換為bool型
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換後的bool類型結果</returns>
        public static bool StrToBool(string expression, bool defValue)
        {
            if (expression == null) return defValue;
            if (string.Compare(expression, "true", StringComparison.OrdinalIgnoreCase) == 0)
                return true;
            return string.Compare(expression, "false", StringComparison.OrdinalIgnoreCase) != 0 && defValue;
        }

7.將對象轉換為Int32類型:

         /// <summary>
        /// 將對象轉換為Int32類型
        /// </summary>
        /// <param name="expression">要轉換的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換後的int類型結果</returns>
        public static int ObjToInt(object expression, int defValue)
        {
            return expression != null ? StrToInt(expression.ToString(), defValue) : defValue;
        }

8.將字符串轉換為Int32類型:

        /// <summary>
        /// 將字符串轉換為Int32類型
        /// </summary>
        /// <param name="expression">要轉換的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換後的int類型結果</returns>
        public static int StrToInt(string expression, int defValue)
        {
            if (string.IsNullOrEmpty(expression) || expression.Trim().Length >= 11 ||
                !System.Text.RegularExpressions.Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                return defValue;

            int rv;
            if (int.TryParse(expression, out rv))
                return rv;

            return Convert.ToInt32(StrToFloat(expression, defValue));
        }

9.Object型轉換為decimal型:

        /// <summary>
        /// Object型轉換為decimal型
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換後的decimal類型結果</returns>
        public static decimal ObjToDecimal(object expression, decimal defValue)
        {
            if (expression != null)
                return StrToDecimal(expression.ToString(), defValue);

            return defValue;
        }

10.string型轉換為decimal型:

        /// <summary>
        /// string型轉換為decimal型
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換後的decimal類型結果</returns>
        public static decimal StrToDecimal(string expression, decimal defValue)
        {
            if ((expression == null) || (expression.Length > 10))
                return defValue;

            decimal intValue = defValue;
            {
                bool isDecimal = System.Text.RegularExpressions.Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (isDecimal)
                    decimal.TryParse(expression, out intValue);
            }
            return intValue;
        }

11.Object型轉換為float型:

        /// <summary>
        /// Object型轉換為float型
        /// </summary>
        /// <param name="strValue">要轉換的字符串</param>
        /// <param name="expression"></param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換後的int類型結果</returns>
        public static float ObjToFloat(object expression, float defValue)
        {
            if (expression != null)
                return StrToFloat(expression.ToString(), defValue);

            return defValue;
        }

12.string型轉換為float型:

        /// <summary>
        /// string型轉換為float型
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換後的int類型結果</returns>
        public static float StrToFloat(string expression, float defValue)
        {
            if ((expression == null) || (expression.Length > 10))
                return defValue;

            float intValue = defValue;
            {
                bool isFloat = System.Text.RegularExpressions.Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (isFloat)
                    float.TryParse(expression, out intValue);
            }
            return intValue;
        }

13.將對象轉換為日期時間類型(提供幾種操作方法份的重載):

        /// <summary>
        /// 將對象轉換為日期時間類型
        /// </summary>
        /// <param name="str">要轉換的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換後的int類型結果</returns>
        public static DateTime StrToDateTime(string str, DateTime defValue)
        {
            if (!string.IsNullOrEmpty(str))
            {
                DateTime dateTime;
                if (DateTime.TryParse(str, out dateTime))
                    return dateTime;
            }
            return defValue;
        }

        /// <summary>
        /// 將對象轉換為日期時間類型
        /// </summary>
        /// <param name="str">要轉換的字符串</param>
        /// <returns>轉換後的int類型結果</returns>
        public static DateTime StrToDateTime(string str)
        {
            return StrToDateTime(str, DateTime.Now);
        }

        /// <summary>
        /// 將對象轉換為日期時間類型
        /// </summary>
        /// <param name="obj">要轉換的對象</param>
        /// <returns>轉換後的int類型結果</returns>
        public static DateTime ObjectToDateTime(object obj)
        {
            return StrToDateTime(obj.ToString());
        }

        /// <summary>
        /// 將對象轉換為日期時間類型
        /// </summary>
        /// <param name="obj">要轉換的對象</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換後的int類型結果</returns>
        public static DateTime ObjectToDateTime(object obj, DateTime defValue)
        {
            return StrToDateTime(obj.ToString(), defValue);
        }

14.將對象轉換為字符串:

        /// <summary>
        /// 將對象轉換為字符串
        /// </summary>
        /// <param name="obj">要轉換的對象</param>
        /// <returns>轉換後的string類型結果</returns>
        public static string ObjectToStr(object obj)
        {
            if (obj == null)
                return "";
            return obj.ToString().Trim();
        }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved