放些有用到或是覺得好用的的Extension Method
如有合適的片段就自行取用吧
部分是自己寫,部分收集自 zzzprojects/Z.ExtensionMethods
依照新增的順序,越後面的是越新的段落
String Extension
字串分割
public static string[] Split(this string @this, string separator, StringSplitOptions option = StringSplitOptions.None) { return @this.Split(new[] { separator }, option); }
字串反轉
public static string Reverse(this string @this) { if (@this.Length <= 1) { return @this; } char[] chars = @this.ToCharArray(); Array.Reverse(chars); return new string(chars); }
字串是否為空
public static bool IsNullOrEmpty(this string @this) { return string.IsNullOrEmpty(@this); }
WebBrowser 的 HtmlDocument Extension
透過 (Html的Class相同) 找出 Elemen
public static HtmlElement GetElementByClassName(this HtmlDocument @this, string className, string tag = null) { HtmlElementCollection allHtmlElements; if (string.IsNullOrEmpty(tag)) allHtmlElements = @this.All; else allHtmlElements = @this.GetElementsByTagName(tag); foreach (HtmlElement tempElement in allHtmlElements) if (tempElement.GetAttribute("className") == className) return tempElement; return null; }
透過 (Html的Class匹配)找出 Element
public static HtmlElement GetElementByMatchClassName(this HtmlDocument @this, string className, string tag = null) { HtmlElementCollection allHtmlElements; if (string.IsNullOrEmpty(tag)) allHtmlElements = @this.All; else allHtmlElements = @this.GetElementsByTagName(tag); foreach (HtmlElement tempElement in allHtmlElements) if (tempElement.GetAttribute("className").Contains(className)) return tempElement; return null; }
透過 (Html的Class相同)找出 所有Element
public static IEnumerable<HtmlElement> GetElementsByClassName(this HtmlDocument @this, string className, string tag = null) { List<HtmlElement> rtnHtmlElements = new List<HtmlElement>(); HtmlElementCollection allHtmlElements; if (string.IsNullOrEmpty(tag)) allHtmlElements = @this.All; else allHtmlElements = @this.GetElementsByTagName(tag); foreach (HtmlElement tempElement in allHtmlElements) if (tempElement.GetAttribute("className") == className) rtnHtmlElements.Add(tempElement); return rtnHtmlElements; }
透過 (Html的Class匹配)找出 所有Element
public static IEnumerable<HtmlElement> GetElementsByMatchClassName(this HtmlDocument @this, string className, string tag = null) { List<HtmlElement> rtnHtmlElements = new List<HtmlElement>(); HtmlElementCollection allHtmlElements; if (string.IsNullOrEmpty(tag)) allHtmlElements = @this.All; else allHtmlElements = @this.GetElementsByTagName(tag); foreach (HtmlElement tempElement in allHtmlElements) if (tempElement.GetAttribute("className").Contains(className)) rtnHtmlElements.Add(tempElement); return rtnHtmlElements; }
透過 (Html的ID和Class相同)找出 Element
public static HtmlElement GetElementByIdAndClassName(this HtmlDocument @this, string id, string className) { var tempElement = @this.GetElementById(id); if (tempElement?.GetAttribute("className") == className) return tempElement; return null; }