.net工具类——对象转换处理
.net工具类——对象转换处理
- GetStrArray
- GetArrayStr
- StrToBool
- ObjToInt
- StrToInt
- ObjToDecimal
- StrToDecimal
- ObjToFloat
- StrToFloat
- ObjToDouble
- StrToDouble
- StrToDateTime
- ObjectToDateTime
- ObjectToStr
#region 对象转换处理 /// <summary> /// 将字符串转换为数组 /// </summary> /// <param name="str">字符串</param> /// <returns>字符串数组</returns> public static string[] GetStrArray(string str) { return str.Split(new char[',']); } /// <summary> /// 将数组转换为字符串 /// </summary> /// <param name="list">List</param> /// <param name="speater">分隔符</param> /// <returns>String</returns> public static string GetArrayStr(List<string> list, string speater) { StringBuilder 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(); } /// <summary> /// object型转换为bool型 /// </summary> /// <param name="strValue">要转换的字符串</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; } /// <summary> /// string型转换为bool型 /// </summary> /// <param name="strValue">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的bool类型结果</returns> public static bool StrToBool(string expression, bool defValue) { if (expression != null) { if (string.Compare(expression, "true", true) == 0) return true; else if (string.Compare(expression, "false", true) == 0) return false; } return defValue; } /// <summary> /// 将对象转换为Int32类型 /// </summary> /// <param name="expression">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的int类型结果</returns> public static int ObjToInt(object expression, int defValue) { if (expression != null) return StrToInt(expression.ToString(), defValue); return defValue; } /// <summary> /// 将字符串转换为Int32类型 /// </summary> /// <param name="expression">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的int类型结果</returns> public static int StrToInt(string expression, int defValue) { int rv; if (Int32.TryParse(expression, out rv)) return rv; return Convert.ToInt32(StrToFloat(expression, defValue)); } /// <summary> /// Object型转换为decimal型 /// </summary> /// <param name="strValue">要转换的字符串</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; } /// <summary> /// string型转换为decimal型 /// </summary> /// <param name="strValue">要转换的字符串</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; if (expression != null) { bool IsDecimal = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); if (IsDecimal) decimal.TryParse(expression, out intValue); } return intValue; } /// <summary> /// Object型转换为float型 /// </summary> /// <param name="strValue">要转换的字符串</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; } /// <summary> /// string型转换为float型 /// </summary> /// <param name="strValue">要转换的字符串</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; if (expression != null) { bool IsFloat = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); if (IsFloat) float.TryParse(expression, out intValue); } return intValue; } /// <summary> /// Object型转换为double型 /// </summary> /// <param name="strValue">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的int类型结果</returns> public static double ObjToDouble(object expression, double defValue) { if (expression != null) return StrToDouble(expression.ToString(), defValue); return defValue; } /// <summary> /// string型转换为double型 /// </summary> /// <param name="strValue">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的int类型结果</returns> public static double StrToDouble(string expression, double defValue) { if ((expression == null) || (expression.Length > 10)) return defValue; double intValue = defValue; if (expression != null) { bool IsDouble = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); if (IsDouble) double.TryParse(expression, out intValue); } return intValue; } /// <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, new DateTime()); } /// <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); } /// <summary> /// 将对象转换为字符串 /// </summary> /// <param name="obj">要转换的对象</param> /// <returns>转换后的string类型结果</returns> public static string ObjectToStr(object obj) { if (obj == null) return ""; return obj.ToString().Trim(); } #endregion 对象转换处理