public static string ExpandAndToString(this IEnumerable src, string separator)
=> string.Join(separator, src.Cast<object>().Select(o => o.ToString()));
// 用法
new[] {1,5,33,14,556}.ExpandAndToString("-") // "1-5-33-14-556"
public static bool IsNullOrEmpty(this string s) => string.IsNullOrEmpty(s);
// 條件執行 Func
public static string IsNullOrEmptyThen(this string s, Func<string,string> func)
=> s.IsNullOrEmpty() ? func(s) : s;
// 條件執行 Action
public static void IsNullOrEmptyThen(this string s, Action<string> act) {
if (s.IsNullOrEmpty()) act(s);
}
// 用法
"".IsNullOrEmptyThen(f => "default") // "default"
"".IsNullOrEmptyThen(f => MessageBox.Show("empty"))
public static string FormatWith(this string fmt, params object[] args)
=> string.Format(fmt, args);
// 用法
"i:{0:0%};x:{1};".FormatWith(0.35, 200) // "i:35%;x:200;"
public static bool In<T>(this T t, params T[] set) => set.Contains(t);
public static bool In<T,C>(this T t, Func<T,C,bool> pred, params C[] set)
=> set.Any(c => pred(t, c));
public static bool InRange<T>(this IComparable<T> t, T min, T max)
=> t.CompareTo(min) >= 0 && t.CompareTo(max) <= 0;
// 用法
95.In(31,3,55,67,95,12,4) // true
"b".InRange("a", "c") // true
95.InRange(15, 100) // true
public static T Trace<T>(this T t, string category = null) {
Debug.WriteLine($"{category ?? "Trace"}: {t}");
return t;
}
public static T Trace<T>(this T t, Func<T,object> selector, string category = null) {
Debug.WriteLine($"{category ?? "Trace"}: {selector(t)}");
return t;
}
public static T TraceFormat<T>(this T t, string fmt, string category = null) {
Debug.WriteLine($"{category ?? "Trace"}: {string.Format(fmt, t)}");
return t;
}
// 用法
"abcdefg".Trace(f => f.ToUpper(), "expr").Remove(4).Trace("normal")
// 輸出: expr: ABCDEFG → normal: abcd
// 基礎結構
public class Switch<T> { ... }
public class Case<T,R> { ... }
// 用法
15.Switch()
.CaseRun(15, f => Msg("=15"), false)
.CaseRun(f => f > 0, f => Msg(">0"))
.CaseRun(f => f < 0, f => Msg("<0"))
.DefaultRun(f => Msg("=0"));
var o = 'c'.Switch()
.CaseReturn('a', 1)
.CaseReturn('b', 2)
.CaseReturn('c', 3)
.CaseReturn('d', 4)
.CaseReturn(f => f > 'd', 5)
.DefaultReturn(0).ReturnValue; // 3
// 單對象遞歸
public static IEnumerable<T> RecursionSelect<T>(this T root, Func<T,IEnumerable<T>> children, Predicate<T> filter = null)
// 集合遞歸
public static IEnumerable<T> RecursionEachSelect<T>(this IEnumerable<T> roots, Func<T,IEnumerable<T>> children, Predicate<T> filter = null)
// 用法:獲取所有有子目錄的目錄
new DirectoryInfo(@"C:\Downloads").RecursionSelect(d => d.GetDirectories(), d => d.GetDirectories().Length > 0)
public static bool RegexIsMatch(this string s, string pattern, RegexOptions opt = RegexOptions.None)
=> Regex.IsMatch(s, pattern, opt);
public static Match RegexMatch(this string s, string pattern, RegexOptions opt = RegexOptions.None)
=> Regex.Match(s, pattern, opt);
public static MatchCollection RegexMatches(this string s, string pattern, RegexOptions opt = RegexOptions.None)
=> Regex.Matches(s, pattern, opt);
public static string RegexReplace(this string s, string pattern, string repl, RegexOptions opt = RegexOptions.None)
=> Regex.Replace(s, pattern, repl, opt);
public static string[] RegexSplit(this string s, string pattern, RegexOptions opt = RegexOptions.None)
=> Regex.Split(s, pattern, opt);
public static PathString AsPathString(this string s) => new PathString(s);
public static UriString AsUriString(this string s) => new UriString(s);
public static XmlString AsXmlString(this string s) => new XmlString(s);
// 等...
// 用法
@"C:\abc\".AsPathString().Combine(@"D:\1.exe".AsPathString().FileName) // "C:\abc\1.exe"
文章來源:skyd.cnblogs.com