Web--TypeConverter
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的更多相关文章
- Struts2中的一个类型转换示例
1.写一个属性文件,里面写好需要转换的类型数据,xwork-conversion.properties,解释: xwork-conversion.properties表示对所有action中的指定数据 ...
- Java五大框架
2017-6-13 Lifusen 此文章仅代表个人观点,如有问题提出请联系Q:570429601 1.Hibernate (开放源代码的对象关系映射框架) Hibernate是一个开放源代码的对象关 ...
- [Web API] Web API 2 深入系列(6) Model绑定(上)
目录 解决什么问题 Model元数据解析 复杂类型 ValueProvider ValueProviderFactory 解决什么问题 Model: Action方法上的参数 Model绑定: 对Ac ...
- 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. 本文主要来讲解以下内容: ...
- ASP.NET Web API中的参数绑定总结
ASP.NET Web API中的action参数类型可以分为简单类型和复杂类型. HttpResponseMessage Put(int id, Product item) id是int类型,是简单 ...
- Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service
实现目标 http://localhost:9000/rs/roomservice 为入口, http://localhost:9000/rs/roomservice/room为房间列表, http: ...
- org.springframework.web.bind.ServletRequestDataBinde
org.springframework.validation Class DataBinder java.lang.Object org.springframework.validation.Data ...
- 自定义TypeConverter把基础类型转换为复杂类型
原文(http://tech.it168.com/d/2008-06-30/200806300953554_all.shtml) TypeConverter对于编写ASP.NET Server Con ...
- Parameter Binding in ASP.NET Web API(参数绑定)
Parameter Binding in ASP.NET Web API(参数绑定) 导航 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnbl ...
- ASP.NET Web API编程——模型验证与绑定
1.模型验证 使用特性约束模型属性 可以使用System.ComponentModel.DataAnnotations提供的特性来限制模型. 例如,Required特性表示字段值不能为空,Range特 ...
随机推荐
- Bugku-CTF加密篇之python(N1CTF) [HRlgC2ReHW1/WRk2DikfNBo1dl1XZBJrRR9qECMNOjNHDktBJSxcI1hZIz07YjVx]
python(N1CTF)
- Bash Game hdu 1846
(一)巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个.最后取光者得胜. 显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先取者 ...
- 吴裕雄 python 机器学习——人工神经网络与原始感知机模型
import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D from ...
- 删除文件时提示,你需来自SYSTEM的权限
1. 提示如下 2. 对要删除的文件操作如下 2.1 为删除的文件添加本地账户 2.2 提示如下,多点几次继续就好 2.3 给本地账户添加完全控制权限
- yii2关闭(开启)csrf的验证
(1)全局使用,我们直接在配置文件中设置enableCookieValidation为true request => [ 'enableCookieValidation' => true, ...
- Windows设置Tomcat的管理员的用户和密码
我们首先打开Tomcat的配置文件,具体如下:(conf目录下的tomcat-users.xml) 删除原有的<tomcat-users>,加入如下代码 <tomcat-users& ...
- jenkins和hudson
自动化构建:Jenkins起源于Hudson.Hudson在商业软件的路上继续前行,而Jenkins则作为开源软件,从hudson分支出来. 因此现在的jenkins和hudson非常类似,但是随着二 ...
- 91云服务器网络带宽测试,IO测试、全国ping测试
91yun服务器测试一键包介绍 一键包主要是为了让大家快速对服务器的基本状况有一个了解.考虑到天朝的网络出口问题,所以这个一键包更加偏向网络的测试. 影响测试耗时主要是下载,整个测试如果是能跑满100 ...
- Jmeter变量嵌套的方法
jmeter中变量的嵌套一般有两种方式 1,调用__V函数 { "phone": "${phone}", "xxId": "${_ ...
- 在虚拟机安装centos7
因为工作需要,要经常用到虚拟机,以I前老让别人给装,可是老问人家也不好,自己整理一份比较适合小白用的教程,有点繁琐: 一.工具:VMware CentOS7 的 ISO 文件 二.开始安装 ...