*
This commit is contained in:
1 parent
5a60ba8a22
commit
c39856f2d6
4 files changed
+43
-19
No files matched your search
@@ -9,7 +9,7 @@ public static class StringExtensions {
|
||||
/// 高速的 StartsWith。
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool StartsWithF([AllowNull] this string value, string prefix, bool ignoreCase = false) {
|
||||
public static bool StartsWithF(this string? value, string prefix, bool ignoreCase = false) {
|
||||
if (value is null) return false;
|
||||
return value.StartsWith(prefix, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
|
||||
}
|
||||
@@ -17,7 +17,7 @@ public static class StringExtensions {
|
||||
/// 高速的 EndsWith。
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EndsWithF([AllowNull] this string value, string suffix, bool ignoreCase = false) {
|
||||
public static bool EndsWithF(this string? value, string suffix, bool ignoreCase = false) {
|
||||
if (value is null) return false;
|
||||
return value.EndsWith(suffix, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
|
||||
}
|
||||
@@ -155,13 +155,7 @@ public static class StringExtensions {
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 将第一个字符转换为大写,其余字符转换为小写。
|
||||
/// </summary>
|
||||
public static string Capitalize(this string word) {
|
||||
if (string.IsNullOrEmpty(word)) return word;
|
||||
return $"{word.Substring(0, 1).Upper()}{word.Substring(1).Lower()}";
|
||||
}
|
||||
#region 替换
|
||||
|
||||
/// <summary>
|
||||
/// 替换字符串中的内容。
|
||||
@@ -170,6 +164,26 @@ public static class StringExtensions {
|
||||
public static string ReplaceOnDemand(this string str, string oldValue, Func<string> newValue) =>
|
||||
str.Contains(oldValue) ? str.Replace(oldValue, newValue()) : str;
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串中的换行符统一替换为指定字符。
|
||||
/// 若指定了 <paramref name="mergeMultiple"/>,会将多次换行合并成一次换行。
|
||||
/// </summary>
|
||||
public static string ReplaceLineEndings([DisallowNull] this string input, string newValue, bool mergeMultiple = false) =>
|
||||
Regex.Replace(input,
|
||||
mergeMultiple ? @"(?:\r\n|[\n\r\f\u0085\u2028\u2029])+" : @"\r\n|[\n\r\f\u0085\u2028\u2029]",
|
||||
newValue.Replace("$", "$$"), // 避免识别成捕获组
|
||||
RegexOptions.Compiled);
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 将第一个字符转换为大写,其余字符转换为小写。
|
||||
/// </summary>
|
||||
public static string Capitalize(this string word) {
|
||||
if (string.IsNullOrEmpty(word)) return word;
|
||||
return $"{word.Substring(0, 1).Upper()}{word.Substring(1).Lower()}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串统一至某个长度。
|
||||
/// 过短则用 <paramref name="code"/> 将其左侧填充,过长则截取靠左的指定长度。
|
||||
@@ -184,14 +198,4 @@ public static class StringExtensions {
|
||||
/// </summary>
|
||||
public static bool IsAsciiOnly([DisallowNull] this string input) => input.All(c => c < 128);
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串中的换行符统一替换为指定字符。
|
||||
/// 若指定了 <paramref name="mergeMultiple"/>,会将多次换行合并成一次换行。
|
||||
/// </summary>
|
||||
public static string ReplaceLineEndings([DisallowNull] this string input, string newValue, bool mergeMultiple = false) =>
|
||||
Regex.Replace(input,
|
||||
mergeMultiple ? @"(?:\r\n|[\n\r\f\u0085\u2028\u2029])+" : @"\r\n|[\n\r\f\u0085\u2028\u2029]",
|
||||
newValue.Replace("$", "$$"), // 避免识别成捕获组
|
||||
RegexOptions.Compiled);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace MeloongCore.Extensions;
|
||||
public static class WpfExtensions {
|
||||
|
||||
public static bool Any(this UIElementCollection? Arr) => Arr?.Count > 0;
|
||||
|
||||
}
|
||||
@@ -22,6 +22,10 @@
|
||||
<PackageReference Include="System.ValueTuple" Version="4.6.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="PresentationFramework" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="System" />
|
||||
<Using Include="System.ComponentModel" />
|
||||
|
||||
@@ -32,41 +32,49 @@ public static class EscapeUtils {
|
||||
/// <summary>
|
||||
/// 正则表达式转义。等同于 Regex.Escape。
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string? RegexEscape(string? value) => Regex.Escape(value);
|
||||
|
||||
/// <summary>
|
||||
/// 正则表达式去转义。等同于 Regex.Unescape。
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string? RegexUnescape(string? value) => Regex.Unescape(value);
|
||||
|
||||
/// <summary>
|
||||
/// URL 转义。等同于 Uri.EscapeDataString。
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string? UrlEscape(string? value) => Uri.EscapeDataString(value);
|
||||
|
||||
/// <summary>
|
||||
/// URL 去转义。等同于 Uri.UnescapeDataString。
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string? UrlUnescape(string? value) => Uri.UnescapeDataString(value);
|
||||
|
||||
/// <summary>
|
||||
/// 表单转义。等同于 WebUtility.UrlEncode。
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string? FormUrlEscape(string? value) => WebUtility.UrlEncode(value);
|
||||
|
||||
/// <summary>
|
||||
/// 表单去转义。等同于 WebUtility.UrlDecode。
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string? FormUrlUnescape(string? value) => WebUtility.UrlDecode(value);
|
||||
|
||||
/// <summary>
|
||||
/// HTML 转义。等同于 WebUtility.HtmlEncode。
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string? HtmlEscape(string? value) => WebUtility.HtmlEncode(value);
|
||||
|
||||
/// <summary>
|
||||
/// HTML 去转义。等同于 WebUtility.HtmlDecode。
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string? HtmlUnescape(string? value) => WebUtility.HtmlDecode(value);
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user