博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
扩展函数之 IsWhat 简单好用
阅读量:4352 次
发布时间:2019-06-07

本文共 5505 字,大约阅读时间需要 18 分钟。

代码实现:

/***扩展函数名细***/ //【IsInRange】 int num = 100;//以前写法if (num > 100 & num < 1000) { }//现在写法if (num.IsInRange(100, 1000)) { } //datetime类型也支持   //【IsNullOrEmpty】object s = "";//以前写法if (s == null || string.IsNullOrEmpty(s.ToString())) { }//现在写法if (s.IsNullOrEmpty()) { }//更顺手了没有 }  //【IsIn】string value = "a";//以前写法我在很多项目中看到if (value == "a" || value == "b" || value == "c") {}//现在写法if (value.IsIn("a", "b", "c")) { } //【IsValuable与IsNullOrEmpty相反】string ss = "";//以前写法if (!string.IsNullOrEmpty(ss)) { }//现在写法if (s.IsValuable()) { }  List
list = null;//以前写法if (list != null && list.Count > 0) { }//现在写法if (list.IsValuable()) { } //IsIDcardif ("32061119810104311x".IsIDcard()){ } //IsTelephoneif ("0513-85669884".IsTelephone()){ } //IsMatch 节约你引用Regex的命名空间了if ("我中国人12".IsMatch(@"人\d{2}")) { } //下面还有很多太简单了的就不介绍了//IsZero//IsInt//IsNoInt//IsMoney//IsEamil//IsMobile

封装类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions; namespace SyntacticSugar{    ///     /// ** 描述:逻辑判段是什么?    /// ** 创始时间:2015-5-29    /// ** 修改时间:-    /// ** 作者:sunkaixuan    /// ** 使用说明:http://www.cnblogs.com/sunkaixuan/p/4539654.html    ///     public static class IsWhat    {        ///         /// 值在的范围?        ///         ///         /// 大于等于begin        /// 小于等于end        /// 
public static bool IsInRange(this int o, int begin, int end) { return o >= begin && o <= end; } /// /// 值在的范围? /// /// /// 大于等于begin /// 小于等于end ///
public static bool IsInRange(this DateTime o, DateTime begin, DateTime end) { return o >= begin && o <= end; } /// /// 在里面吗? /// ///
/// /// ///
public static bool IsIn
(this T o, params T[] values) { return values.Contains(o); } ///
/// 是null或""? /// ///
public static bool IsNullOrEmpty(this object o) { if (o == null || o == DBNull.Value) return true; return o.ToString() == ""; } ///
/// 是null或""? /// ///
public static bool IsNullOrEmpty(this Guid? o) { if (o == null) return true; return o == Guid.Empty; } ///
/// 是null或""? /// ///
public static bool IsNullOrEmpty(this Guid o) { if (o == null) return true; return o == Guid.Empty; } ///
/// 有值?(与IsNullOrEmpty相反) /// ///
public static bool IsValuable(this object o) { if (o == null) return false; return o.ToString() != ""; } ///
/// 有值?(与IsNullOrEmpty相反) /// ///
public static bool IsValuable(this IEnumerable
o) { if (o == null || o.Count() == 0) return false; return true; } /// /// 是零? /// /// ///
public static bool IsZero(this object o) { return (o == null || o.ToString() == "0"); } /// /// 是INT? /// /// ///
public static bool IsInt(this object o) { if (o == null) return false; return Regex.IsMatch(o.ToString(), @"^\d+$"); } /// /// 不是INT? /// /// ///
public static bool IsNoInt(this object o) { if (o == null) return true; return !Regex.IsMatch(o.ToString(), @"^\d+$"); } /// /// 是金钱? /// /// ///
public static bool IsMoney(this object o) { if (o == null) return false; double outValue = 0; return double.TryParse(o.ToString(), out outValue); } /// /// 是邮箱? /// /// ///
public static bool IsEamil(this object o) { if (o == null) return false; return Regex.IsMatch(o.ToString(), @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"); } /// /// 是手机? /// /// ///
public static bool IsMobile(this object o) { if (o == null) return false; return Regex.IsMatch(o.ToString(), @"^\d{11}$"); } /// /// 是座机? /// public static bool IsTelephone(this object o) { if (o == null) return false; return System.Text.RegularExpressions.Regex.IsMatch(o.ToString(), @"^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}$"); } /// /// 是身份证? /// /// ///
public static bool IsIDcard(this object o) { if (o == null) return false; return System.Text.RegularExpressions.Regex.IsMatch(o.ToString(), @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$"); } /// ///是适合正则匹配? /// /// /// 大于等于begin /// 小于等于end ///
public static bool IsMatch(this object o, string pattern) { if (o == null) return false; Regex reg = new Regex(pattern); return reg.IsMatch(o.ToString()); } }}

 

转载于:https://www.cnblogs.com/jameslif/p/6831455.html

你可能感兴趣的文章
jquip,更简洁的代码
查看>>
【OJ】PAT-A解题报告
查看>>
文档语法
查看>>
利用套接字实现进程通信一例
查看>>
linux中shell变量$#,$@,$0,$1,$2的含义解释
查看>>
常用的shell命令整理
查看>>
A Brief Introduction to the Design of UBIFS
查看>>
了解你的Linux系统:必须掌握的20个命令
查看>>
js setInterval 启用&停止
查看>>
knockoutJS学习笔记04:监控属性
查看>>
18.10.6 考试总结
查看>>
iptables防火墙网路安全实践配置
查看>>
ASP.net Web窗体添加多条数据到数据库
查看>>
PHP面向对象(三)
查看>>
mysql与实际时间有8小时差怎么办
查看>>
docker 常用命令
查看>>
微信小程序 - 参数传递
查看>>
在Centos7上安装Oracle
查看>>
Linux下启动/关闭Oracle
查看>>
session和cookie的区别
查看>>