misc
This commit is contained in:
1 parent
501180c9e8
commit
c017654b6a
3 files changed
+66
-43
No files matched your search
@@ -96,10 +96,11 @@ public static class StringExtensions {
|
||||
/// 若原始字符串为空,则返回 {""}。
|
||||
/// </summary>
|
||||
public static string[] Split(this string fullStr, string splitStr) {
|
||||
if (splitStr.IsSingle())
|
||||
if (splitStr.IsSingle()) {
|
||||
return fullStr.Split(splitStr[0]);
|
||||
else
|
||||
} else {
|
||||
return fullStr.Split([splitStr], StringSplitOptions.None);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -108,10 +109,8 @@ public static class StringExtensions {
|
||||
/// </summary>
|
||||
public static string BeforeFirst(this string str, [AllowNull] 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;
|
||||
if (pos < 0) return str;
|
||||
return str.Substring(0, pos);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取在子字符串最后一次出现之前的部分,例如对 2024/11/08 拆切 / 会得到 2024/11。
|
||||
@@ -119,10 +118,8 @@ public static class StringExtensions {
|
||||
/// </summary>
|
||||
public static string BeforeLast(this string str, [AllowNull] 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;
|
||||
if (pos < 0) return str;
|
||||
return str.Substring(0, pos);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -131,10 +128,8 @@ public static class StringExtensions {
|
||||
/// </summary>
|
||||
public static string AfterFirst(this string str, [AllowNull] 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;
|
||||
if (pos < 0) return str;
|
||||
return str.Substring(pos + text!.Length);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取在子字符串最后一次出现之后的部分,例如对 2024/11/08 拆切 / 会得到 08。
|
||||
@@ -142,10 +137,8 @@ public static class StringExtensions {
|
||||
/// </summary>
|
||||
public static string AfterLast(this string str, [AllowNull] 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;
|
||||
if (pos < 0) return str;
|
||||
return str.Substring(pos + text!.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -155,18 +148,20 @@ public static class StringExtensions {
|
||||
/// </summary>
|
||||
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)
|
||||
if (startPos >= 0) {
|
||||
startPos += after.Length;
|
||||
else
|
||||
} else {
|
||||
startPos = 0;
|
||||
}
|
||||
|
||||
int endPos = string.IsNullOrEmpty(before) ? -1 : str.IndexOfF(before, startPos, ignoreCase);
|
||||
if (endPos >= 0)
|
||||
if (endPos >= 0) {
|
||||
return str.Substring(startPos, endPos - startPos);
|
||||
else if (startPos > 0)
|
||||
} else if (startPos > 0) {
|
||||
return str.Substring(startPos);
|
||||
else
|
||||
} else {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -175,20 +170,20 @@ public static class StringExtensions {
|
||||
|
||||
/// <summary>
|
||||
/// 替换字符串中的内容。
|
||||
/// 仅当需要替换时,才调用 <paramref name="newValue"/> 获取结果字符串。
|
||||
/// 仅当需要替换时,才调用 <paramref name="newValueGetter"/> 获取结果字符串。
|
||||
/// </summary>
|
||||
public static string ReplaceOnDemand(this string str, string oldValue, Func<string> newValue) =>
|
||||
str.Contains(oldValue) ? str.Replace(oldValue, newValue()) : str;
|
||||
public static string Replace(this string str, string oldValue, Func<string> newValueGetter) =>
|
||||
str.Contains(oldValue) ? str.Replace(oldValue, newValueGetter()) : str;
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串中的换行符统一替换为指定字符。
|
||||
/// 若指定了 <paramref name="mergeMultiple"/>,会将多个连续换行符仅替换为一个目标内容。
|
||||
/// </summary>
|
||||
public static string ReplaceLineEndings(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);
|
||||
public static string ReplaceLineEndings(this string input, string newValue, bool mergeMultiple = false) {
|
||||
return (mergeMultiple ? regexLineEndingAndMerge : regexLineEnding).Replace(input, newValue.Replace("$", "$$")); // 避免识别成捕获组
|
||||
}
|
||||
private static readonly Regex regexLineEndingAndMerge = new(@"(?:\r\n|[\n\r\f\u0085\u2028\u2029])+", RegexOptions.Compiled);
|
||||
private static readonly Regex regexLineEnding = new(@"\r\n|[\n\r\f\u0085\u2028\u2029]", RegexOptions.Compiled);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
Reference in new issue
Block a user