C#類型轉換類(通用類)


//
     /// 類型轉換類
     /// 處理數據庫獲取字段為空的情況
     ///
     public  static class DBConvert
     {
         #region------------------ToInt32類型轉換------------------
         ///
         /// 讀取數據庫中字符串並轉換成Int32
         /// 為空時返回0
         ///
         /// object類型的值
         /// Int32類型
         public  static int ToInt32(object obj)
         {
             int  result = 0;
             if  (IsInt(Convert.ToString(obj)))
             {
                 result = Convert.ToInt32(obj);
             }
             else  if (obj != null && obj is Enum) //處理非null值類型時(或者枚舉)
             {
                 result = ((IConvertible)obj).ToInt32( null );
             }
             return  result;
         }
 
         ///
         /// 讀取數據庫中字符串並轉換成Int32
         /// 為空時返回0
         ///
         /// string類型的值
         /// Int32類型
         public  static int ToInt32(string str)
         {
             int  result = 0;
             if  (IsInt(str))
             {
                 result = Convert.ToInt32(str);
             }
             return  result;
         }
 
         ///
         /// 判斷一個字符串是否屬於Int類型
         /// 如果是的返回true,如果不是返回false
         ///
         /// string類型的值
         /// true:是Int的字符串(即可以轉換成Int類型),false:不是Int類型的字符串
         public  static bool IsInt(string str)
         {
             bool  result = false;
             if  (str != "" && str!=null)
             {
                 Regex reg =  new Regex("^[0-9]*$");
                 if  (reg.IsMatch(str))
                 {
                     result =  true;
                 }
             }
             return  result;
         }
         #endregion
 
         #region------------------ToString類型轉換------------------
         ///
         ///  讀取數據庫中字符串並轉換成string
         ///
         /// object類型的值
         /// string類型
         public  static string ToString(object obj)
         {
             string  result = "";
             if  (obj != null)
             {
                 result = Convert.ToString(obj);
             }
             return  result;
         }
         #endregion
 
         #region------------------ToDouble類型轉換------------------
         ///
         /// 判斷一個字符串是否屬於Double類型(包括負浮點型)
         /// 如果是的返回true,如果不是返回false
         ///
         /// string類型的值
         /// true:是Double的字符串(即可以轉換成Double類型),false:不是Double類型的字符串
         public  static bool IsDouble(string str)
         {
             bool  result = false;
             if  (str != "" && str != null)
             {
                 Regex reg =  new Regex(@"^(-?\d+)(\.\d+)?$");
                 if  (reg.IsMatch(str))
                 {
                     result =  true;
                 }
             }
             return  result;
         }
 
         ///
         /// 讀取數據庫中字符串並轉換成Int32
         /// 為空時返回0
         ///
         /// object類型的值
         /// Int32類型
         public  static double ToDouble(object obj)
         {
             double  result = 0.0;
             if  (IsDouble(Convert.ToString(obj)))
             {
                 result = Convert.ToDouble(obj);
             }
             else  if (obj != null && obj is Enum) //處理枚舉
             {
                 result = ((IConvertible)obj).ToDouble( null );
             }
             return  result;
         }
 
         ///
         /// 讀取數據庫中字符串並轉換成Int32
         /// 為空時返回0
         ///
         /// string類型的值
         /// Int32類型
         public  static double ToDouble(string str)
         {
             double  result = 0.0;
             if  (IsDouble(str))
             {
                 result = Convert.ToDouble(str);
             }
             return  result;
         }
         #endregion
 
         #region------------------ToDateTime類型轉換------------------
         ///
         /// 判斷時間格式是否是時間類型
         /// 如23:10
         ///
         /// 要判斷的字符串
         /// true:是時間類型的字符串(即可以轉換成時間類型),false:不是時間類型的字符串
         public  static bool isDateTime(string str)
         {
             bool  result = false;
             if  (str != "" && str != null)
             {
                 Regex reg =  new Regex("(([01]\\d)|(2[0-3])):[0-5]\\d");
                 if  (reg.IsMatch(str))
                 {
                     result =  true;
                 }
             }
             return  result;
         }
         #endregion
 
     }
}
//"^\d+(\.\d+)?$"  //非負浮點數(正浮點數 + 0)
//"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮點數
//"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"  //非正浮點數(負浮點數 + 0)
//"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //負浮點數
//"^(-?\d+)(\.\d+)?$"  //浮點數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM