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;
}
}
}

Web--TypeConverter的更多相关文章

  1. Struts2中的一个类型转换示例

    1.写一个属性文件,里面写好需要转换的类型数据,xwork-conversion.properties,解释: xwork-conversion.properties表示对所有action中的指定数据 ...

  2. Java五大框架

    2017-6-13 Lifusen 此文章仅代表个人观点,如有问题提出请联系Q:570429601 1.Hibernate (开放源代码的对象关系映射框架) Hibernate是一个开放源代码的对象关 ...

  3. [Web API] Web API 2 深入系列(6) Model绑定(上)

    目录 解决什么问题 Model元数据解析 复杂类型 ValueProvider ValueProviderFactory 解决什么问题 Model: Action方法上的参数 Model绑定: 对Ac ...

  4. Asp.Net Web API 2第十六课——Parameter Binding in ASP.NET Web API(参数绑定)

    导航 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html. 本文主要来讲解以下内容: ...

  5. ASP.NET Web API中的参数绑定总结

    ASP.NET Web API中的action参数类型可以分为简单类型和复杂类型. HttpResponseMessage Put(int id, Product item) id是int类型,是简单 ...

  6. Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service

    实现目标 http://localhost:9000/rs/roomservice 为入口, http://localhost:9000/rs/roomservice/room为房间列表, http: ...

  7. org.springframework.web.bind.ServletRequestDataBinde

    org.springframework.validation Class DataBinder java.lang.Object org.springframework.validation.Data ...

  8. 自定义TypeConverter把基础类型转换为复杂类型

    原文(http://tech.it168.com/d/2008-06-30/200806300953554_all.shtml) TypeConverter对于编写ASP.NET Server Con ...

  9. Parameter Binding in ASP.NET Web API(参数绑定)

    Parameter Binding in ASP.NET Web API(参数绑定) 导航 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnbl ...

  10. ASP.NET Web API编程——模型验证与绑定

    1.模型验证 使用特性约束模型属性 可以使用System.ComponentModel.DataAnnotations提供的特性来限制模型. 例如,Required特性表示字段值不能为空,Range特 ...

随机推荐

  1. gbase安装教程

    一.安装前的准备工作 1.对网卡进行配置 [root@gbase8a ~]#vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 HWADD ...

  2. hadoop fs -put could only be replicated to 0 nodes, instead of 1 解决方法

    我的坏境是在虚拟机linux操作系统中,启动start-all.sh后 1.执行jps,如下 2.执行hadoop fs -mkdir input 创建成功 执行hadoop fs -ls 可以看到i ...

  3. AcWing 868. 筛质数 线性筛法

    #include <iostream> #include <algorithm> using namespace std; ; int primes[N], cnt; bool ...

  4. jmeter的BeanShell Sampler使用--导入第三方jar包

    实现目的 测试接口的过程中,可能有时需要用到第三方jar包来生成一些测试数据,此时我们就可以通过BeanShell来调用自己编写的工具类,来对jmeter的功能进行扩展,以满足测试需要. 脚本实现 在 ...

  5. ASP.NET CSRF 解决【网摘】

    http://stackoverflow.com/questions/29939566/preventing-cross-site-request-forgery-csrf-attacks-in-as ...

  6. spring 基于xml的申明式AspectH中的后置通知的返回值获取

    spring 基于xml的申明式AspectH中的后置通知的返回值获取 1. 配置文件 <aop:config> <aop:aspect ref="myAspect&quo ...

  7. ECMAScript 6.0

    ES6怎么来的 ECMAScript 和 JavaScript ECMA 是标准,JS 是实现 ECMAScript 简称 ECMA 或 ES 历史版本 1996, ES1.0 Netscape 将 ...

  8. java中路径的问题

    在java中,涉及路径的问题有很多,不管在windows还是linux系统中,不要纠结”/“分隔符的使用,在windows系统中,资源加载器会自动的将”/“转换成”\“. 在java中获取资源的方式有 ...

  9. 拿到别人的Django程序如何在本地RUN起来

    在Pycharm IDE下 Edit Configurations 1.检查Python interpreter 2.检查 Working directory 3.Settings 数据库配置

  10. Hack the box邀请码和注册问题总结

    注意下,有3个坑, 1. 解码方式是随机的,记得看DATA下面提示用哪种 2. post时候可以直接用f12里的console,命令是: $.post('https://www.hackthebox. ...