• Asp.Net类型转换类(通用类)


        /// <summary>
        /// 类型转换类
        /// 处理数据库获取字段为空的情况
        /// </summary>
        public static class DBConvert
        {
            #region------------------ToInt32类型转换------------------
            /// <summary>
            /// 读取数据库中字符串并转换成Int32
            /// 为空时返回0
            /// </summary>
            /// <param name="obj">object类型的值</param>
            /// <returns>Int32类型</returns>
            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;
            }
    
            /// <summary>
            /// 读取数据库中字符串并转换成Int32
            /// 为空时返回0
            /// </summary>
            /// <param name="str">string类型的值</param>
            /// <returns>Int32类型</returns>
            public static int ToInt32(string str)
            {
                int result = 0;
                if (IsInt(str))
                {
                    result = Convert.ToInt32(str);
                }
                return result;
            }
    
            /// <summary>
            /// 判断一个字符串是否属于Int类型
            /// 如果是的返回true,如果不是返回false
            /// </summary>
            /// <param name="str">string类型的值</param>
            /// <returns>true:是Int的字符串(即可以转换成Int类型),false:不是Int类型的字符串</returns>
            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类型转换------------------
            /// <summary>
            ///  读取数据库中字符串并转换成string
            /// </summary>
            /// <param name="obj">object类型的值</param>
            /// <returns>string类型</returns>
            public static string ToString(object obj)
            {
                string result = "";
                if (obj != null)
                {
                    result = Convert.ToString(obj);
                }
                return result;
            }
            #endregion
    
            #region------------------ToDouble类型转换------------------
            /// <summary>
            /// 判断一个字符串是否属于Double类型(包括负浮点型)
            /// 如果是的返回true,如果不是返回false
            /// </summary>
            /// <param name="str">string类型的值</param>
            /// <returns>true:是Double的字符串(即可以转换成Double类型),false:不是Double类型的字符串</returns>
            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;
            }
    
            /// <summary>
            /// 读取数据库中字符串并转换成Int32
            /// 为空时返回0
            /// </summary>
            /// <param name="obj">object类型的值</param>
            /// <returns>Int32类型</returns>
            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;
            }
    
            /// <summary>
            /// 读取数据库中字符串并转换成Int32
            /// 为空时返回0
            /// </summary>
            /// <param name="str">string类型的值</param>
            /// <returns>Int32类型</returns>
            public static double ToDouble(string str)
            {
                double result = 0.0;
                if (IsDouble(str))
                {
                    result = Convert.ToDouble(str);
                }
                return result;
            }
            #endregion
    
            #region------------------ToDateTime类型转换------------------
            /// <summary>
            /// 判断时间格式是否是时间类型
            /// 如23:10
            /// </summary>
            /// <param name="str">要判断的字符串</param>
            /// <returns>true:是时间类型的字符串(即可以转换成时间类型),false:不是时间类型的字符串</returns>
            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+)?$"  //浮点数 
    

      

  • 相关阅读:
    LinkedList源码解析
    四种List实现类的对比总结
    HashMap源码解析
    Flutter——Switch组件(开关组件)
    Flutter——Radio组件、RadioListTile组件(单选按钮组件)
    Flutter——Checkbox组件、CheckboxListTile(多选框组件)
    Flutter——TextField组件(文本框组件)
    Flutter——FloatingActionButton组件(浮动按钮组件)
    Flutter中的按钮组件介绍
    Flutter——Drawer、DrawerHeader、UserAccountsDrawerHeader组件(侧边栏组件)
  • 原文地址:https://www.cnblogs.com/ranlin/p/2129885.html
Copyright © 2020-2023  润新知