diff --git a/Extensions/Enumerable.cs b/Extensions/EnumerableExtensions.cs similarity index 81% rename from Extensions/Enumerable.cs rename to Extensions/EnumerableExtensions.cs index 9da9c8e..ef9c19b 100644 --- a/Extensions/Enumerable.cs +++ b/Extensions/EnumerableExtensions.cs @@ -1,11 +1,13 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Text; namespace MeloongCore.Extensions { public static class EnumerableExtensions { - #region 去重 + #region Distinct /// /// 通过比较器进行去重。 @@ -28,7 +30,7 @@ namespace MeloongCore.Extensions { #endregion - #region 取最大、最小值 + #region MinBy / MaxBy /// /// 选择所有最大值对应的对象。 @@ -122,6 +124,42 @@ namespace MeloongCore.Extensions { #endregion + #region Join + + /// + /// 用指定的分割符将集合连接为字符串。 + /// + public static string Join(this IEnumerable list, char split) { + StringBuilder builder = new StringBuilder(); + bool isFirst = true; + foreach (var element in list) { + if (isFirst) + isFirst = false; + else + builder.Append(split); + if (element != null) builder.Append(element.ToString()); + } + return builder.ToString(); + } + /// + /// 用指定的分割符将集合连接为字符串。 + /// + public static string Join(this IEnumerable list, string split) { + if (split.IsSingle()) return list.Join(split[0]); + StringBuilder builder = new StringBuilder(); + bool isFirst = true; + foreach (var element in list) { + if (isFirst) + isFirst = false; + else + builder.Append(split); + if (element != null) builder.Append(element.ToString()); + } + return builder.ToString(); + } + + #endregion + /// /// 判断集合是否有且仅有一个元素。 /// diff --git a/Extensions/String.cs b/Extensions/String.cs deleted file mode 100644 index 95169b3..0000000 --- a/Extensions/String.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using System.Runtime.CompilerServices; - -namespace MeloongCore.Extensions { - public static class StringExtensions { - - /// - /// 高速的 StartsWith。 - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool StartsWithF(this string value, string prefix, bool ignoreCase = false) { - if (value == null) return false; - return value.StartsWith(prefix, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); - } - /// - /// 高速的 EndsWith。 - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool EndsWithF(this string value, string suffix, bool ignoreCase = false) { - if (value == null) return false; - return value.EndsWith(suffix, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); - } - - /// - /// 忽略大小写的 Contains。 - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool ContainsIgnoreCase(this string value, string subString) { - return value.IndexOf(subString, StringComparison.OrdinalIgnoreCase) >= 0; - } - - /// - /// 高速的 IndexOf。 - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int IndexOfF(this string value, string subString, bool ignoreCase = false) { - return value.IndexOf(subString, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); - } - /// - /// 高速的 IndexOf。 - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int IndexOfF(this string value, string subString, int startIndex, bool ignoreCase = false) { - return value.IndexOf(subString, startIndex, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); - } - - /// - /// 高速的 LastIndexOf。 - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int LastIndexOfF(this string value, string subString, bool ignoreCase = false) { - return value.LastIndexOf(subString, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); - } - /// - /// 高速的 LastIndexOf。 - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int LastIndexOfF(this string value, string subString, int startIndex, bool ignoreCase = false) { - return value.LastIndexOf(subString, startIndex, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); - } - - } -} diff --git a/Extensions/StringExtensions.cs b/Extensions/StringExtensions.cs new file mode 100644 index 0000000..52b9242 --- /dev/null +++ b/Extensions/StringExtensions.cs @@ -0,0 +1,168 @@ +using System; +using System.Runtime.CompilerServices; + +namespace MeloongCore.Extensions { + public static class StringExtensions { + + #region 区域性 / 大小写 简化 + + /// + /// 高速的 StartsWith。 + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool StartsWithF(this string value, string prefix, bool ignoreCase = false) { + if (value == null) return false; + return value.StartsWith(prefix, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); + } + /// + /// 高速的 EndsWith。 + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool EndsWithF(this string value, string suffix, bool ignoreCase = false) { + if (value == null) return false; + return value.EndsWith(suffix, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); + } + + /// + /// 忽略大小写的 Contains。 + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool ContainsIgnoreCase(this string value, string subString) { + return value.IndexOf(subString, StringComparison.OrdinalIgnoreCase) >= 0; + } + + /// + /// 高速的 IndexOf。 + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int IndexOfF(this string value, string subString, bool ignoreCase = false) { + return value.IndexOf(subString, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); + } + /// + /// 高速的 IndexOf。 + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int IndexOfF(this string value, string subString, int startIndex, bool ignoreCase = false) { + return value.IndexOf(subString, startIndex, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); + } + + /// + /// 高速的 LastIndexOf。 + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int LastIndexOfF(this string value, string subString, bool ignoreCase = false) { + return value.LastIndexOf(subString, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); + } + /// + /// 高速的 LastIndexOf。 + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int LastIndexOfF(this string value, string subString, int startIndex, bool ignoreCase = false) { + return value.LastIndexOf(subString, startIndex, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); + } + + /// + /// ToLowerInvariant 的简略写法。 + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string Lower(this string str) => str.ToLowerInvariant(); + /// + /// ToUpperInvariant 的简略写法。 + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string Upper(this string str) => str.ToUpperInvariant(); + + #endregion + + #region 分割与裁切 + + /// + /// 分割字符串。 + /// 若原始字符串为空,则返回 {""}。 + /// + public static string[] Split(this string fullStr, string splitStr) { + if (splitStr.IsSingle()) + return fullStr.Split(splitStr[0]); + else + return fullStr.Split(new[] {splitStr}, StringSplitOptions.None); + } + + /// + /// 获取在子字符串第一次出现之前的部分,例如对 2024/11/08 拆切 / 会得到 2024。 + /// 如果未找到子字符串则不裁切。 + /// + public static string BeforeFirst(this string str, string text, bool ignoreCase = false) { + int pos = string.IsNullOrEmpty(text) ? -1 : str.IndexOfF(text, ignoreCase); + if (pos >= 0) + return str.Substring(0, pos); + else + return str; + } + /// + /// 获取在子字符串最后一次出现之前的部分,例如对 2024/11/08 拆切 / 会得到 2024/11。 + /// 如果未找到子字符串则不裁切。 + /// + public static string BeforeLast(this string str, string text, bool ignoreCase = false) { + int pos = string.IsNullOrEmpty(text) ? -1 : str.LastIndexOfF(text, ignoreCase); + if (pos >= 0) + return str.Substring(0, pos); + else + return str; + } + + /// + /// 获取在子字符串第一次出现之后的部分,例如对 2024/11/08 拆切 / 会得到 11/08。 + /// 如果未找到子字符串则不裁切。 + /// + public static string AfterFirst(this string str, string text, bool ignoreCase = false) { + int pos = string.IsNullOrEmpty(text) ? -1 : str.IndexOfF(text, ignoreCase); + if (pos >= 0) + return str.Substring(pos + text.Length); + else + return str; + } + /// + /// 获取在子字符串最后一次出现之后的部分,例如对 2024/11/08 拆切 / 会得到 08。 + /// 如果未找到子字符串则不裁切。 + /// + public static string AfterLast(this string str, string text, bool ignoreCase = false) { + int pos = string.IsNullOrEmpty(text) ? -1 : str.LastIndexOfF(text, ignoreCase); + if (pos >= 0) + return str.Substring(pos + text.Length); + else + return str; + } + + /// + /// 获取处于两个子字符串之间的部分,裁切尽可能多的内容。 + /// 等效于 AfterLast 后接 BeforeFirst。 + /// 如果未找到子字符串则不裁切。 + /// + public static string Between(this string str, string after, string before, bool ignoreCase = false) { + int startPos = string.IsNullOrEmpty(after) ? -1 : str.LastIndexOfF(after, ignoreCase); + if (startPos >= 0) + startPos += after.Length; + else + startPos = 0; + + int endPos = string.IsNullOrEmpty(before) ? -1 : str.IndexOfF(before, startPos, ignoreCase); + if (endPos >= 0) + return str.Substring(startPos, endPos - startPos); + else if (startPos > 0) + return str.Substring(startPos); + else + return str; + } + + #endregion + + /// + /// 将第一个字符转换为大写,其余字符转换为小写。 + /// + public static string Capitalize(this string word) { + if (string.IsNullOrEmpty(word)) return word; + return word.Substring(0, 1).Upper() + word.Substring(1).Lower(); + } + + } +}