.Net扩展方法集合.

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

在项目中很多时候都会对字符串和集合做特定的处理。而且很多地方都会去调用。为了解决这些问题。我们通常会在项目中吧这些方法提成公共方法。下面是自己总结的项目中用到的一些扩展方法。封装成了一个Library放到github上.

https://github.com/FourLeafClover/LightWeight.Extensions

不多说我们来看看扩展的一些方法吧.

publicstaticvoidMain(string[] args)

{#regionString Extensionsstringresult0 ="[frank%loch".StringConvert4SQLLke(); //替换sql模糊匹配的时候里面的特殊字符

boolresult1 ="abC".CurrentTrimEqualsIgnoreCase("ABC"); // 比较两字符串是否相同,忽略大小写,字符串前后空白

List<string> result2 ="1-23@56*78".SplitByMutipleChar('-','@','*').ToList(); //多字母拆分字符串, 输出 { 1 23 56 78 }的集合

boolresult3 ="123".IsNullOrEmpty(); // 判断字符串是否为null或者string.empty.

boolresult4 ="123".IsLengthBetween(2,4);

boolresult5 ="123".IsShorterThan(2);

boolresult6 ="123".IsLongerThan(2);

boolresult7 ="You are the best".IsContainsAll("You","the");// result==true,result8==false IsContainsAll 支持多参数输入. 用于判断字符串是否同时包含多个字符串boolresult8 ="You are the best".IsContainsAll("You","123");

boolresult9 ="You are the best".IsContainsAny("123","the"); //result==true,result8==false IsContainsAll 支持多参数输入. 用于判断字符串是否包含多个字符串中的一个

boolresult10 ="You are the best".IsContainsAny("sbc","123");

stringresult11 ="frank".FirstCharToUpter();

stringresult12 ="frank".RTrim(); // 去除字符串右边的空白

stringresult13 ="frank".LTrim(); // 去除字符串左边空白

stringresult14 =newList<string>() {"frank","loch","hardy"}.ListToSqlIn(false); //用于sql里面的in(x,x,x)语句拼接stringresult15 =newList<string>() {"1","2","3"}.ListToSqlIn(true); // result12 = "'frank','loch','hardy'",result12 = "1,2,3"

boolresult16 ="frank".IsEqualsAnyOfThem("hardy","frank"); // IsEqualsAnyOfItem支持多参数输入,判断字符串是否在多个删除里面#endregion#regionConvertExtensions // 用于string类型转换int,decimal,datetimeintresult17 ="abc".TryToInt32();intresult18 ="123".TryToInt32();decimalresult19 ="1.3".TryToDecimal();

DateTime result20="2015/12/24".TryToDateTime();#endregion#regionObjectExtensionsPerson p1=newPerson() { Name ="Frank"};

Person result21= p1.DeepCopy<Person>(); // 深拷贝#endregion#regionList<string> lst =newList<string>() {"123","456"};boolresult22 =lst.IsNullOrEmpty(); //判断集合是否有内容boolresult23 =lst.CountGreaterThanZero();

List<Person> personList1 =newList<Person>()

{newPerson(){Name="Frank"},newPerson(){Name="Hardy"}

};

List<Person> personList2 =newList<Person>()

{newPerson(){Name="Frank"},newPerson(){Name="Hardy"}

};

///public static bool ObjectCollectionEquals<T>(this IEnumerable<T> collection1, IEnumerable<T> collection2, Func<T, T, bool> func),

/// func里面写入两个对象相同的方法.实现集合对象的比较boolresult24 = personList1.ObjectCollectionEquals(personList2, (val1, val2) =>{returnval1.Name !=val2.Name;

});#endregion}

[Serializable]publicclassPerson

{publicstringName {get;set; }

}

PS:代码都很简单. 就不剖析了. 各位博友谁还有比较常用的扩展方法都可以提出来. 我好更新到github上.这样可以不断的去完善这个library.

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航