对象转换处理
Posted nxj1997
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对象转换处理相关的知识,希望对你有一定的参考价值。
#region 对象转换处理
/// <summary>
/// 判断对象是否为Int32类型的数字
/// </summary>
/// <param name="Expression"></param>
/// <returns></returns>
public static bool IsNumeric(object expression)
{
if (expression != null)
return IsNumeric(expression.ToString());
/// <summary>
/// 判断对象是否为Int32类型的数字
/// </summary>
/// <param name="Expression"></param>
/// <returns></returns>
public static bool IsNumeric(object expression)
{
if (expression != null)
return IsNumeric(expression.ToString());
return false;
}
/// <summary>
/// 判断对象是否为Int32类型的数字
/// </summary>
/// <param name="Expression"></param>
/// <returns></returns>
public static bool IsNumeric(string expression)
{
if (expression != null)
{
string str = expression;
if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
{
if ((str.Length < 10) || (str.Length == 10 && str[0] == ‘1‘) || (str.Length == 11 && str[0] == ‘-‘ && str[1] == ‘1‘))
return true;
}
}
return false;
}
/// 判断对象是否为Int32类型的数字
/// </summary>
/// <param name="Expression"></param>
/// <returns></returns>
public static bool IsNumeric(string expression)
{
if (expression != null)
{
string str = expression;
if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
{
if ((str.Length < 10) || (str.Length == 10 && str[0] == ‘1‘) || (str.Length == 11 && str[0] == ‘-‘ && str[1] == ‘1‘))
return true;
}
}
return false;
}
/// <summary>
/// 是否为Double类型
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public static bool IsDouble(object expression)
{
if (expression != null)
return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(.w*)?$");
/// 是否为Double类型
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public static bool IsDouble(object expression)
{
if (expression != null)
return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(.w*)?$");
return false;
}
}
/// <summary>
/// 检测是否符合email格式
/// </summary>
/// <param name="strEmail">要判断的email字符串</param>
/// <returns>判断结果</returns>
public static bool IsValidEmail(string strEmail)
{
return Regex.IsMatch(strEmail, @"^[w.]+([-]w+)*@[A-Za-z0-9-_]+[.][A-Za-z0-9-_]");
}
public static bool IsValidDoEmail(string strEmail)
{
return Regex.IsMatch(strEmail, @"^@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
}
/// 检测是否符合email格式
/// </summary>
/// <param name="strEmail">要判断的email字符串</param>
/// <returns>判断结果</returns>
public static bool IsValidEmail(string strEmail)
{
return Regex.IsMatch(strEmail, @"^[w.]+([-]w+)*@[A-Za-z0-9-_]+[.][A-Za-z0-9-_]");
}
public static bool IsValidDoEmail(string strEmail)
{
return Regex.IsMatch(strEmail, @"^@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
}
/// <summary>
/// 检测是否是正确的Url
/// </summary>
/// <param name="strUrl">要验证的Url</param>
/// <returns>判断结果</returns>
public static bool IsURL(string strUrl)
{
return Regex.IsMatch(strUrl, @"^(http|https)://([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9-]+.)*[a-zA-Z0-9-]+.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(:[0-9]+)*(/($|[a-zA-Z0-9.,?‘\+&%$#=~_-]+))*$");
}
/// 检测是否是正确的Url
/// </summary>
/// <param name="strUrl">要验证的Url</param>
/// <returns>判断结果</returns>
public static bool IsURL(string strUrl)
{
return Regex.IsMatch(strUrl, @"^(http|https)://([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9-]+.)*[a-zA-Z0-9-]+.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(:[0-9]+)*(/($|[a-zA-Z0-9.,?‘\+&%$#=~_-]+))*$");
}
/// <summary>
/// 将字符串转换为数组
/// </summary>
/// <param name="str">字符串</param>
/// <returns>字符串数组</returns>
public static string[] GetStrArray(string str)
{
return str.Split(new char[‘,‘]);
}
/// 将字符串转换为数组
/// </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>
/// <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);
/// 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;
}
/// 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);
/// 将对象转换为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)
{
if (string.IsNullOrEmpty(expression) || expression.Trim().Length >= 11 || !Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(.w*)?$"))
return defValue;
/// 将字符串转换为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 || !Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(.w*)?$"))
return defValue;
int rv;
if (Int32.TryParse(expression, out rv))
return 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);
/// 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;
/// 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;
}
if (expression != null)
{
bool IsDecimal = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(.w*)?$");
if (IsDecimal)
decimal.TryParse(expression, out intValue);
}
return intValue;
}
/// <summary>
/// 保留小数
/// </summary>
public static decimal Round(decimal Value, int decimals)
{
if (decimals == 0) return Value;
string format = "0.";
for (int i = 0; i < decimals; i++)
{
format += "0";
}
Value = Convert.ToDecimal(Value.ToString(format));
return Value;
}
/// 保留小数
/// </summary>
public static decimal Round(decimal Value, int decimals)
{
if (decimals == 0) return Value;
string format = "0.";
for (int i = 0; i < decimals; i++)
{
format += "0";
}
Value = Convert.ToDecimal(Value.ToString(format));
return Value;
}
/// <summary>
/// 千克转磅型转换为decimal型
/// </summary>
/// <param name="Value">重量</param>
/// <returns>转换后的磅结果</returns>
public static decimal KgToLb(decimal Value)
{
return Value * 2.20462262185M;
}
/// <summary>
/// 千克转磅型转换为decimal型
/// </summary>
/// <param name="Value">重量</param>
/// <returns>转换后的磅结果</returns>
public static decimal LbToKg(decimal Value)
{
return Value * 0.4535924M;
}
/// <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);
/// 千克转磅型转换为decimal型
/// </summary>
/// <param name="Value">重量</param>
/// <returns>转换后的磅结果</returns>
public static decimal KgToLb(decimal Value)
{
return Value * 2.20462262185M;
}
/// <summary>
/// 千克转磅型转换为decimal型
/// </summary>
/// <param name="Value">重量</param>
/// <returns>转换后的磅结果</returns>
public static decimal LbToKg(decimal Value)
{
return Value * 0.4535924M;
}
/// <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;
/// 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>
/// 从字符串中取出数字
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetNumberInString(string value)
{
Regex regex = new Regex(@"[0-9]*", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.Singleline);
MatchCollection matchCollection = regex.Matches(value);
string str = "";
foreach (Match match in matchCollection)
{
str += match.Value;
}
return str;
}
if (expression != null)
{
bool IsFloat = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(.w*)?$");
if (IsFloat)
float.TryParse(expression, out intValue);
}
return intValue;
}
/// <summary>
/// 从字符串中取出数字
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetNumberInString(string value)
{
Regex regex = new Regex(@"[0-9]*", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.Singleline);
MatchCollection matchCollection = regex.Matches(value);
string str = "";
foreach (Match match in matchCollection)
{
str += match.Value;
}
return str;
}
/// <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>
/// <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>
/// <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>
/// <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>
/// <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();
}
/// <summary>
/// 获得字符串中开始和结束字符串中间得值
/// </summary>
/// <param name="str">字符串</param>
/// <param name="s">开始</param>
/// <param name="e">结束</param>
/// <returns></returns>
public static string GetAmongValue(string str, string s, string e)
{
Regex rg = new Regex("(?<=" + s + ").+?(?=" + e + ")", RegexOptions.Multiline | RegexOptions.Singleline);
return rg.Match(str).Value;
}
#endregion
/// 将对象转换为字符串
/// </summary>
/// <param name="obj">要转换的对象</param>
/// <returns>转换后的string类型结果</returns>
public static string ObjectToStr(object obj)
{
if (obj == null)
return "";
return obj.ToString().Trim();
}
/// <summary>
/// 获得字符串中开始和结束字符串中间得值
/// </summary>
/// <param name="str">字符串</param>
/// <param name="s">开始</param>
/// <param name="e">结束</param>
/// <returns></returns>
public static string GetAmongValue(string str, string s, string e)
{
Regex rg = new Regex("(?<=" + s + ").+?(?=" + e + ")", RegexOptions.Multiline | RegexOptions.Singleline);
return rg.Match(str).Value;
}
#endregion
以上是关于对象转换处理的主要内容,如果未能解决你的问题,请参考以下文章
FastJsonJacksonGson进行Java对象转换Json的细节处理
Atitit 跨平台异常处理--------异常转换 -----java c# js异常对象结构比较and转换