/// <summary>
/// 转换
/// </summary>
public static class ConversionHelper
{
#region 数据格式转换
/// <summary>
/// 转换成Int
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static int ToInt(this object inputValue)
{
return inputValue.IsInt() ? int.Parse(inputValue.ToStringValue()) : 0;
} /// <summary>
/// 转换成Int32
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static int ToInt32(this object inputValue)
{
return inputValue.IsInt() ? Convert.ToInt32(inputValue) : 0;
} /// <summary>
/// 转换成Int64
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static long ToInt64(this object inputValue)
{
return inputValue.IsInt() ? Convert.ToInt64(inputValue) : 0;
} /// <summary>
/// 转换成Double
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static Double ToDouble(this object inputValue)
{
return !inputValue.IsNullOrEmpty() ? Convert.ToDouble(inputValue) : 0;
} /// <summary>
/// 转换成Int64
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool? ToBool(this object inputValue)
{
return inputValue.IsNullOrEmpty() ? (bool?)null : bool.Parse(inputValue.ToStringValue());
} /// <summary>
/// 转换obj 成string
/// </summary>
/// <param name="inputValue">object</param>
/// <returns></returns>
public static string ToStringValue(this object inputValue)
{
return inputValue == null ? "" : inputValue.ToString();
} /// <summary>
/// 转换成数据库字符串
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static string ToDbString(this object inputValue)
{
return !inputValue.IsNullOrEmpty() ? "'" + inputValue.ToStringValue().Trim().Replace("'", "''") + "'" : "''";
} /// <summary>
/// 转换成时间类型yyyy-MM-dd
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static DateTime ToDateTime(this object inputValue)
{
return inputValue.IsNullOrEmpty() ? DateTime.MinValue : Convert.ToDateTime(inputValue);
} /// <summary>
/// 转换成时间类型
/// </summary>
/// <param name="inputValue">输入值</param>
/// <param name="format">时间格式</param>
/// <returns></returns>
public static DateTime ToDateTime(this object inputValue, string format)
{
return DateTime.ParseExact(inputValue.ToStringValue(), format, null);
}
#endregion
}
/// <summary>
/// 验证
/// </summary>
public static class VerificationHelper
{
#region 正则表达式
//邮政编码
private static readonly Regex RegPostCode = new Regex("^\\d{6}$");
//中国身份证验证
private static readonly Regex RegCardId = new Regex("^\\d{17}[\\d|X]|\\d{15}|\\d{18}$");
//数字
private static readonly Regex RegNumber = new Regex("^\\d+$");
//固定电话
private static readonly Regex RegTel = new Regex("^\\d{3,4}-\\d{7,8}|\\d{7,8}$");
//手机号
private static readonly Regex RegPhone = new Regex("^[1][3-8]\\d{9}$");
//电话号码(包括固定电话和手机号)
private static readonly Regex RegTelePhone = new Regex("^(\\d{3,4}-\\d{7,8}|\\d{7,8})|([1][3-8]\\d{9})$");
//邮箱
private static readonly Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");
//中文
private static readonly Regex RegChzn = new Regex("[\u4e00-\u9fa5]");
//IP地址
private static readonly Regex RegIp = new Regex("((25[0-5]|2[0-4]\\d|1?\\d?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1?\\d?\\d)");
#endregion #region 验证方法
/// <summary>
/// 判断是否是数字
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsInt(this object inputValue)
{
int num;
return int.TryParse(inputValue.ToStringValue(), out num);
}
/// <summary>
/// 判断是否是小数
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsDouble(this object inputValue)
{
Double dValue;
return Double.TryParse(inputValue.ToStringValue(), out dValue);
}
/// <summary>
/// 判断是否是小数
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsFloat(this object inputValue)
{
float fValue;
return float.TryParse(inputValue.ToStringValue(), out fValue);
}
/// <summary>
/// 判断字符串是否为空
/// 空:true,不为空:false
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsNullOrEmpty(this object inputValue)
{
return string.IsNullOrEmpty(inputValue.ToStringValue());
} /// <summary>
/// 判断字符串是否为Email
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsEmail(this object inputValue)
{
var match = RegEmail.Match(inputValue.ToStringValue());
return match.Success;
}
/// <summary>
/// 判断字符串是否为固话
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsTel(this object inputValue)
{
var match = RegTel.Match(inputValue.ToStringValue());
return match.Success;
}
/// <summary>
/// 判断字符串是否为手机号
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsPhone(this object inputValue)
{
var match = RegPhone.Match(inputValue.ToStringValue());
return match.Success;
}
/// <summary>
/// 判断字符串是否为电话号码
/// (包含固定电话和手机号)
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsTelePhone(this object inputValue)
{
var match = RegTelePhone.Match(inputValue.ToStringValue());
return match.Success;
}
/// <summary>
/// 判断字符串是否为IP地址
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsIp(this object inputValue)
{
var match = RegIp.Match(inputValue.ToStringValue());
return match.Success;
}
/// <summary>
/// 判断字符串是否为邮编
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsPostCode(this object inputValue)
{
var match = RegPostCode.Match(inputValue.ToStringValue());
return match.Success;
}
/// <summary>
/// 判断字符串是否为身份证
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsCardId(this object inputValue)
{
var match = RegCardId.Match(inputValue.ToStringValue());
return match.Success;
}
/// <summary>
/// 判断字符串是否为中文
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsChzn(this object inputValue)
{
var match = RegChzn.Match(inputValue.ToStringValue());
return match.Success;
}
/// <summary>
/// 判断字符串是否为数字
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool IsNumber(this object inputValue)
{
var match = RegNumber.Match(inputValue.ToStringValue());
return match.Success;
}
#endregion
}

C#常用扩展方法的更多相关文章

  1. WebAPi添加常用扩展方法及思维发散

    前言 在WebAPi中我们通常需要得到请求信息中的查询字符串或者请求头中数据再或者是Cookie中的数据,如果需要大量获取,此时我们应该想到封装一个扩展类来添加扩展方法,从而实现简便快捷的获取. We ...

  2. Javascript 常用扩展方法

    这篇文章纯粹是为了保存这些方法,供以后翻阅,其实一直保存在 evernote 里面,但觉得还是放到对的地方会好点. 现在收录的很少,希望以后会慢慢增多. 数组扩展 contains,remove 扩展 ...

  3. js常用扩展方法

    在日常的开发过程中,经常会碰到javaScript原生对象方法不够用的情况,所以经常会对javaScript原生方法进行扩展.下面就是在实际工作时,经常使用的一些方法,做一下记录,有需要的可以拿去. ...

  4. ES6之字符串扩展方法(常用)

    es6这个String对象倒是扩展了不少方法,但是很多都是跟字符编码相关,个人选了几个感觉比较常用的方法: includes 搜索字符的神器 还记得我们之前如何判断某个字符串对象是否包含特地字符的吗? ...

  5. C# 一些常用的字符串扩展方法

    以下可能是常用的.net扩展方法,记录下 EString.cs文件 /// <summary> /// 扩展字符串类 /// </summary> public static ...

  6. Farseer.net轻量级开源框架 中级篇:常用的扩展方法

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: BasePage.BaseController.BaseHandler.BaseM ...

  7. ES6 模版字符串及常用的es6扩展方法

    1.ES6 模版字符串es6 模版字符串主要用于简化字符串的拼接 <script type="text/javascript"> let obj={name:'rdb' ...

  8. ES6 对象定义简写及常用的扩展方法

    1.ES6 对象定义简写 es6提供了对象定义里的属性,方法简写方式: 假如属性和变量名一样,可以省略,包括定义对象方法function也可以省略 <script type="text ...

  9. C#语法糖: 扩展方法(常用)

    今天继续分享C#4.0语法糖的扩展方法,这个方法也是我本人比较喜欢的方法.大家先想想比如我们以前写的原始类型不能满足现在的需求,而需要在该类型中添加新的方法来实现时大家会怎么做.我先说一下我没有学习到 ...

随机推荐

  1. java基础(59):Eclipse把自动括号匹配改成C++那样的(强迫症,没办法)

    在Eclipse开发环境下依次选择: 1.Windows->Preferences->Java->Code Style->Formatter: 2.在Active Profil ...

  2. SQL中索引的原理

    (一)深入浅出理解索引结构         实际上,您可以把索引理解为一种特殊的目录.微软的SQL   SERVER提供了两种索引:聚集索引(clustered   index,也称聚类索引.簇集索引 ...

  3. jQuery Ajax 简单的实现跨域请求

    html 代码清单: <script type="text/javascript" src="http://www.youxiaju.com/js/jquery-1 ...

  4. 夺命雷公狗---DEDECMS----1dedecms的安装过程

    我们这次要玩的dedecms(cms也就是内容管理系统),电商网.或者政府网..小说网.新闻网之类的都是基于可以用cms来实现的. 现在在市场上主流的cms系统有dedecms(织梦),帝国cms,p ...

  5. [php] PHPExcel插入图片

    其它的代码就不贴了,直接上关键代码: $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $objActSh ...

  6. zw版【转发·台湾nvp系列Delphi例程】HALCON DirectShow (Delphi Prism)

    zw版[转发·台湾nvp系列Delphi例程]HALCON DirectShow (Delphi Prism) namespace DirectShow_Prism;interfaceuses Sys ...

  7. Java图像文件的读写

    读取bmp文件到BufferedImage中File file2 = new File("c:\\testimages\\tttt" + ".bmp");// ...

  8. 锋利的JQuery(一)

    释义: Ajax:Asynchronous Javascript And XML,异步的Javascript和XML 其它库: Prototype:最早 Dojo:学习曲线陡 YUI:比较丰富 Ext ...

  9. 字符串核对之Boyer-Moore算法

    算法说明: 在计算机科学里,Boyer-Moore字符串搜索算法是一种非常高效的字符串搜索算法.它由Bob Boyer和J Strother Moore设计于1977年.此算法仅对搜索目标字符串(关键 ...

  10. MySQL OnlineDDL

    参考资料: http://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html http://www.mysqlperfo ...