.NET實用擴展方法詳解
持續(xù)更新的.NET實用擴展方法,具體內(nèi)容如下
1. 字符串轉(zhuǎn)換為可空數(shù)值類型(int, long, float...類似)
/// <summary> /// 將字符串轉(zhuǎn)換成32位整數(shù),轉(zhuǎn)換失敗返回null /// </summary> /// <param name="str">轉(zhuǎn)換的字符串</param> /// <returns>轉(zhuǎn)換之后的整數(shù),或null</returns> public static int? TryParseToInt32(this string str) { if (string.IsNullOrWhiteSpace(str)) return null; var result = 0; if (int.TryParse(str, out result)) return result; else return null; }
2. 去除子字符串
/// <summary> /// 去除子字符串 /// </summary> /// <param name="str">原字符串</param> /// <param name="substring">要去除的字符串</param> /// <returns>去除子字符串之后的結(jié)果</returns> public static string DeSubstring(this string str, string substring) { if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(substring) || !str.Contains(substring)) { return str; } return Regex.Replace(str, Regex.Escape(substring), string.Empty); } /// <summary> /// 去除子字符串 /// </summary> /// <param name="str">原字符串</param> /// <param name="substrings">要去除的子字符串</param> /// <returns>去除子字符串之后的結(jié)果</returns> public static string DeSubstring(this string str, params string[] substrings) { if (string.IsNullOrEmpty(str)) return str; if (substrings == null) return str; var newStr = str; foreach (var item in substrings) { newStr = newStr.DeSubstring(item); } return newStr; }
3. 獲取子序列
/// <summary> /// 獲取子序列 /// </summary> /// <typeparam name="T">序列中元素類型</typeparam> /// <param name="source">源數(shù)據(jù)</param> /// <param name="startIndex">開始索引(返回時包括)</param> /// <param name="endIndex">結(jié)束索引(返回時包括)</param> /// <returns>子序列</returns> public static IEnumerable<T> SubEnumerable<T>(this IEnumerable<T> source, int startIndex, int endIndex) { if (source == null) yield return default(T); var length = source.Count(); if (startIndex < 0 || endIndex < startIndex || startIndex >= length || endIndex >= length) throw new ArgumentOutOfRangeException(); var index = -1; foreach (var item in source) { index++; if (index < startIndex) continue; if (index > endIndex) yield break; yield return item; } }
4. 通過指定鍵對序列去重, 不必實現(xiàn)IEqualityComparer接口
/// <summary> /// 通過對指定的值進行比較返回序列中的非重復(fù)元素。 /// </summary> /// <typeparam name="T">序列中元素類型</typeparam> /// <typeparam name="TResult">指定的比較屬性類型</typeparam> /// <param name="source">源數(shù)據(jù)</param> /// <param name="selector">應(yīng)用于每個元素的轉(zhuǎn)換函數(shù)</param> /// <returns>一個包含源序列中的按指定屬性非重復(fù)元素</returns> public static IEnumerable<T> Distinct<T, TResult>(this IEnumerable<T> source, Func<T, TResult> selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); var set = new HashSet<TResult>(); foreach (var item in source) { var result = selector(item); if (set.Add(result)) { yield return item; } } }
5. 獲取序列中重復(fù)的元素序列, 原理和去重類似
/// <summary> /// 通過對指定的值進行比較返回序列中重復(fù)的元素 /// </summary> /// <typeparam name="T">序列中的數(shù)據(jù)類型</typeparam> /// <typeparam name="TResult">指定的比較屬性類型</typeparam> /// <param name="source">源數(shù)據(jù)</param> /// <param name="selector">應(yīng)用于每個元素的轉(zhuǎn)換函數(shù)</param> /// <returns>一個包含源序列中的按指定元素的重復(fù)元素</returns> public static IEnumerable<T> Identical<T>(this IEnumerable<T> source) { if (source == null) throw new ArgumentNullException(nameof(source)); var setT = new HashSet<T>(); foreach (var item in source) { if (!setT.Add(item)) { yield return item; } } } /// <summary> /// 通過對指定的值進行比較返回序列中重復(fù)的元素 /// </summary> /// <typeparam name="T">序列中的數(shù)據(jù)類型</typeparam> /// <typeparam name="TResult">指定的比較屬性類型</typeparam> /// <param name="source">源數(shù)據(jù)</param> /// <param name="selector">應(yīng)用于每個元素的轉(zhuǎn)換函數(shù)</param> /// <returns>一個包含源序列中的按指定元素的重復(fù)元素</returns> public static IEnumerable<T> Identical<T, TResult>(this IEnumerable<T> source, Func<T, TResult> selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); var setTResult = new HashSet<TResult>(); foreach (var item in source) { var result = selector(item); if (!setTResult.Add(result)) { yield return item; } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET?MVC5網(wǎng)站開發(fā)之用戶添加和瀏覽2(七)
這篇文章主要為大家詳細(xì)介紹了ASP.NET?MVC5網(wǎng)站開發(fā)之用戶添加和瀏覽,感興趣的小伙伴們可以參考一下2016-08-08Equals和==的區(qū)別 公共變量和屬性的區(qū)別小結(jié)
Equals 和==的區(qū)別 公共變量和屬性的區(qū)別 總結(jié)一下。2009-11-11有關(guān).NET參數(shù)傳遞的方式引發(fā)的思考
在.NET中參數(shù)的使用方法主要為可選參數(shù)、命名參數(shù)、可變數(shù)量參數(shù)等等。本文也是主要介紹這三種參數(shù)的使用方法2016-12-12xls表格導(dǎo)入數(shù)據(jù)庫功能實例代碼
這篇文章介紹了xls表格導(dǎo)入數(shù)據(jù)庫功能實例代碼,有需要的朋友可以參考一下2013-10-10asp.net core3.1 引用的元包dll版本兼容性問題解決方案
這篇文章主要介紹了asp.net core 3.1 引用的元包dll版本兼容性問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03詳解CentOS 7.4下如何部署Asp.Net Core結(jié)合consul
這篇文章主要介紹了詳解CentOS 7.4下如何部署Asp.Net Core結(jié)合consul,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06Asp.net內(nèi)置對象之Server對象(概述及應(yīng)用)
Server對象提供對服務(wù)器上的方法和屬性的訪問以及進行HTML編碼的功能,本文主要圍繞server對象介紹詳細(xì)功能及常用屬性和主要方法,感興趣的朋友可以了解下,或許對你學(xué)習(xí)server對象有所幫助2013-02-02