欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#、ASP.NET通用工具类IsWhat?(可以判断数字、身份证、数据类型等等)

程序员文章站 2022-06-21 09:46:53
好东西都需要人去整理、分类 注意:需要引用命名空间 syntacticsugar 用法: /***扩展函数名细***/ //【isinrange】...

好东西都需要人去整理、分类

注意:需要引用命名空间 syntacticsugar

用法:

/***扩展函数名细***/
 
//【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<string> list = null;
//以前写法
if (list != null && list.count > 0) { }
//现在写法
if (list.isvaluable()) { }
 
 
 
 
//isidcard
if ("32061119810104311x".isidcard())
{
 
}
 
//istelephone
if ("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
{
  /// <summary>
  /// ** 描述:逻辑判段是什么?
  /// ** 创始时间:2015-5-29
  /// ** 修改时间:-
  /// ** 作者:sunkaixuan
  /// </summary>
  public static class iswhat
  {
    /// <summary>
    /// 值在的范围?
    /// </summary>
    /// <param name="o"></param>
    /// <param name="begin">大于等于begin</param>
    /// <param name="end">小于等于end</param>
    /// <returns></returns>
    public static bool isinrange(this int o, int begin, int end)
    {
      return o >= begin && o <= end;
    }
    /// <summary>
    /// 值在的范围?
    /// </summary>
    /// <param name="o"></param>
    /// <param name="begin">大于等于begin</param>
    /// <param name="end">小于等于end</param>
    /// <returns></returns>
    public static bool isinrange(this datetime o, datetime begin, datetime end)
    {
      return o >= begin && o <= end;
    }
 
    /// <summary>
    /// 在里面吗?
    /// </summary>
    /// <typeparam name="t"></typeparam>
    /// <param name="o"></param>
    /// <param name="values"></param>
    /// <returns></returns>
    public static bool isin<t>(this t o, params t[] values)
    {
      return values.contains(o);
    }
 
    /// <summary>
    /// 是null或""?
    /// </summary>
    /// <returns></returns>
    public static bool isnullorempty(this object o)
    {
      if (o == null || o == dbnull.value) return true;
      return o.tostring() == "";
    }
    /// <summary>
    /// 是null或""?
    /// </summary>
    /// <returns></returns>
    public static bool isnullorempty(this guid? o)
    {
      if (o == null) return true;
      return o == guid.empty;
    }
    /// <summary>
    /// 是null或""?
    /// </summary>
    /// <returns></returns>
    public static bool isnullorempty(this guid o)
    {
      if (o == null) return true;
      return o == guid.empty;
    }
 
    /// <summary>
    /// 有值?(与isnullorempty相反)
    /// </summary>
    /// <returns></returns>
    public static bool isvaluable(this object o)
    {
      if (o == null) return false;
      return o.tostring() != "";
    }
    /// <summary>
    /// 有值?(与isnullorempty相反)
    /// </summary>
    /// <returns></returns>
    public static bool isvaluable(this ienumerable<object> o)
    {
      if (o == null || o.count() == 0) return false;
      return true;
    }
 
    /// <summary>
    /// 是零?
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public static bool iszero(this object o)
    {
      return (o == null || o.tostring() == "0");
    }
 
    /// <summary>
    /// 是int?
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public static bool isint(this object o)
    {
      if (o == null) return false;
      return regex.ismatch(o.tostring(), @"^\d+$");
    }
    /// <summary>
    /// 不是int?
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public static bool isnoint(this object o)
    {
      if (o == null) return true;
      return !regex.ismatch(o.tostring(), @"^\d+$");
    }
 
    /// <summary>
    /// 是金钱?
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public static bool ismoney(this object o)
    {
      if (o == null) return false;
      double outvalue = 0;
      return double.tryparse(o.tostring(), out outvalue);
    }
 
    /// <summary>
    /// 是邮箱?
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public static bool iseamil(this object o)
    {
      if (o == null) return false;
      return regex.ismatch(o.tostring(), @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");
    }
 
    /// <summary>
    /// 是手机?
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public static bool ismobile(this object o)
    {
      if (o == null) return false;
      return regex.ismatch(o.tostring(), @"^\d{11}$");
    }
 
    /// <summary>
    /// 是座机?
    /// </summary>
    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}$");
 
    }
 
    /// <summary>
    /// 是身份证?
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    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))$");
    }
 
    /// <summary>
    ///是适合正则匹配?
    /// </summary>
    /// <param name="o"></param>
    /// <param name="begin">大于等于begin</param>
    /// <param name="end">小于等于end</param>
    /// <returns></returns>
    public static bool ismatch(this object o, string pattern)
    {
      if (o == null) return false;
      regex reg = new regex(pattern);
      return reg.ismatch(o.tostring());
    }
  }
}