/// <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. 如何不切换windows登陆用户,更换用户名访问共享文件夹

    @echo offnet use * /del /ynet use \\192.168.2.1 /user:wr@echo off 先进行删除所有,然后在进行映射,按照部门来,不同的部门可能需要映射的 ...

  2. Opencv读取各种格式图片,在TBitmap上面重绘

    //opencv读取图片 cv::Mat image; //const char *fileName = "HeadImage-UI/Photo-001.bmp"; const c ...

  3. eclipse批量删除断点(转)

    1.首先调出BreakPoints选项卡(Window--show View--Other--BreakPoints). 2.选择BreakPoints选项卡,选择所有断点,点击删除即可. 

  4. Fresco源码解析 - DataSource怎样存储数据

    Fresco源码解析 - DataSource怎样存储数据 datasource是一个独立的 package,与FB导入的guava包都在同一个工程内 - fbcore. datasource的类关系 ...

  5. Struts2中<jsp:forward page="xxx.action"></jsp:forward>失效

    问题:在Struts2中<jsp:forward page="xxx.action"></jsp:forward>失效了,不但调转不过去还报404错误.不知 ...

  6. PTPX中的report 选项

    Report的生成 report_power表示产生power report,update_power表示进行power analysis. report_power命令可以生成四种形式的report ...

  7. zw版【转发·台湾nvp系列Delphi例程】HALCON DivImage1

    zw版[转发·台湾nvp系列Delphi例程]HALCON DivImage1 procedure TForm1.Button1Click(Sender: TObject);var    img0, ...

  8. C语言初学者代码中的常见错误与瑕疵(13)

    https://www.cpfn.org/bbs/viewtopic.php?f=85&t=5940&sid=ccbcf716d21191452e7c08a97b502337& ...

  9. Elasticsearch--Date math在索引中的使用

    在Elasticsearch,有时要通过索引日期来筛选某段时间的数据,这时就要用到ES提供的日期数学表达式 描述: 特别在日志数据中,只是查询一段时间内的日志数据,这时就可以使用日期数学表达式,这样可 ...

  10. Delphi xe 下快捷使用 FastMM 的内存泄露检测功能

    Delphi xe 集成了FastMM,调试程序是的时候可以方便地检查内存泄露了.  使用方法:在project中,添加一行: ReportMemoryLeaksOnShutdown := Debug ...