using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; namespace Xima.FreamWork.Common.Web
{
public class TypeConverter
{ /// <summary>
/// string型转换为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) == || string.Compare(expression, "on", true) == )
return true;
else if (string.Compare(expression, "false", true) == || string.Compare(expression, "off", true) == )
return false;
}
return defValue;
} /// <summary>
/// obj转换成string型
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public static string ObjToStr(object expression)
{
if (expression != null)
{
return Convert.ToString(expression);
}
return string.Empty;
} /// <summary>
/// 将对象转换为Int32类型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static int ObjectToInt(object expression)
{
return ObjectToInt(expression, );
} /// <summary>
/// 将对象转换为Int32类型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static int ObjectToInt(object expression, int defValue)
{
if (expression != null)
return StrToInt(expression.ToString(), defValue); return defValue;
} /// <summary>
/// 将对象转换为Int32类型,转换失败返回0
/// </summary>
/// <param name="str">要转换的字符串</param>
/// <returns>转换后的int类型结果</returns>
public static int StrToInt(string str)
{
return StrToInt(str, );
} /// <summary>
/// 将对象转换为Byte类型
/// </summary>
/// <param name="expression">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static Byte ObjectToByte(object expression, Byte defValue)
{
if (expression != null)
return StrToByte(expression.ToString(), defValue);
return defValue;
} /// <summary>
/// 将对象转换为Byte类型
/// </summary>
/// <param name="str">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static Byte StrToByte(string str, Byte defValue)
{
if (string.IsNullOrEmpty(str) || str.Trim().Length > || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
return defValue;
Byte rv;
if (Byte.TryParse(str, out rv))
return rv;
return rv == ? defValue : Convert.ToByte(StrToFloat(str, defValue));
} /// <summary>
/// 将对象转换为Int32类型
/// </summary>
/// <param name="str">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static int StrToInt(string str, int defValue)
{
if (string.IsNullOrEmpty(str) || str.Trim().Length >= || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
return defValue; int rv;
if (Int32.TryParse(str, out rv))
return rv; return Convert.ToInt32(StrToFloat(str, defValue));
} /// <summary>
/// 将对象转换为Int32类型
/// </summary>
/// <param name="str">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static Int64 StrToInt64(string str, int defValue)
{
if (string.IsNullOrEmpty(str) || str.Trim().Length >= || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
return defValue; Int64 rv;
if (Int64.TryParse(str, out rv))
return rv;
return Convert.ToInt64(StrToFloat(str, defValue));
} /// <summary>
/// string型转换为float型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static float StrToFloat(object strValue, float defValue)
{
if ((strValue == null))
return defValue; return StrToFloat(strValue.ToString(), defValue);
} /// <summary>
/// string型转换为float型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static float ObjectToFloat(object strValue, float defValue)
{
if ((strValue == null))
return defValue; return StrToFloat(strValue.ToString(), defValue);
} /// <summary>
/// string型转换为float型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static float ObjectToFloat(object strValue)
{
return ObjectToFloat(strValue.ToString(), );
} /// <summary>
/// string型转换为float型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <returns>转换后的int类型结果</returns>
public static float StrToFloat(object strValue)
{
if ((strValue == null))
return ; return StrToFloat(strValue.ToString(), );
} /// <summary>
/// string型转换为float型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static float StrToFloat(string strValue, float defValue)
{
if ((strValue == null) || (strValue.Length > ))
return defValue; float intValue = defValue;
{
var IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
if (IsFloat)
float.TryParse(strValue, out intValue);
}
return intValue;
} /// <summary>
/// string型转换为Decimal型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的Decimal类型结果</returns>
public static decimal StrToDecimal(string strValue, decimal defValue)
{
if ((strValue == null) || (strValue.Length > ))
return defValue; decimal intValue = defValue;
if (strValue != null)
{
bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
if (IsFloat)
decimal.TryParse(strValue, out intValue);
}
return intValue;
}
}
}

最新文章

  1. webapp开发调试环境--weinre配置
  2. Inside Flask - flask.__init__.py 和核心组件
  3. 团队作业php
  4. 网页clientWidth等相关
  5. MySQL增删改查的常用操作指令总结
  6. 转的git
  7. dom元素和方法总结
  8. Apache Mina开发手冊之四
  9. 设计模式--静态工厂设计模式在android中的使用
  10. 公众号第三方平台开发-aes解密失败
  11. c++(排序二叉树删除)
  12. 1 Expression of Possiblity
  13. 洛谷P3620 数据备份
  14. jquery判断checkBox的checked
  15. C++ 将数据转为字符串的几种方法
  16. nginx-access.log的logstash解析
  17. wpf下使用NotifyIcon
  18. Python爬虫实战二之爬取百度贴吧帖子
  19. Android 中 DrawerLayout + ViewPager 怎么解决滑动冲突?
  20. Web后台模拟前端post(带NTLM验证)

热门文章

  1. bbs系统的相关知识点
  2. 微信个人支付接口---YunGouOS 1.1.3 版本发布,新增个人微信/支付宝收款接口
  3. 常见的一些mysql多表操作收集(备份)
  4. mongodb的一些操作
  5. 题解 P1717 【钓鱼】
  6. 【转】Docker网络模式--默认模式bridge模式
  7. php源码加密--screw plus
  8. Ubuntu国内镜像
  9. 刷题15. 3Sum
  10. dateTimepicker 设置默认日期的方法