GFramework/GFramework.Core/Extensions/StringExtensions.cs
gewuyou 4740d30fb7 fix(core): 修复 PR 评审指出的编译与样式问题
- 修复 AsyncExtensionsTests 中错误返回 ConfiguredTaskAwaitable 导致的测试编译失败

- 收敛多处测试中的冗余 async/await 与 ValueTask 断言包装,减少 PR review 指出的告警

- 更新 StoreSelection 的 net9+ 锁实现与 analyzer-warning-reduction 跟踪文档,记录 PR #288 与当前 MSB4018 环境阻塞
2026-04-25 13:03:17 +08:00

93 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace GFramework.Core.Extensions;
/// <summary>
/// 字符串扩展方法
/// </summary>
public static class StringExtensions
{
/// <summary>
/// 如果字符串为空,则返回 null否则返回原字符串
/// </summary>
/// <param name="str">要检查的字符串</param>
/// <returns>如果字符串为空则返回 null否则返回原字符串</returns>
/// <example>
/// <code>
/// string text = "";
/// var result = text.NullIfEmpty(); // 返回 null
/// </code>
/// </example>
public static string? NullIfEmpty(this string? str)
{
return string.IsNullOrEmpty(str) ? null : str;
}
/// <summary>
/// 截断字符串到指定的最大长度,并可选地添加后缀
/// </summary>
/// <param name="str">要截断的字符串</param>
/// <param name="maxLength">最大长度(包括后缀)</param>
/// <param name="suffix">截断时添加的后缀,默认为 "..."</param>
/// <returns>截断后的字符串</returns>
/// <exception cref="ArgumentNullException">当 str 为 null 时抛出</exception>
/// <exception cref="ArgumentOutOfRangeException">当 maxLength 小于后缀长度时抛出</exception>
/// <example>
/// <code>
/// var text = "Hello World";
/// var truncated = text.Truncate(8); // "Hello..."
/// </code>
/// </example>
public static string Truncate(this string str, int maxLength, string suffix = "...")
{
if (str is null)
{
throw new ArgumentNullException(nameof(str));
}
if (suffix is null)
{
throw new ArgumentNullException(nameof(suffix));
}
if (maxLength < suffix.Length)
{
throw new ArgumentOutOfRangeException(nameof(maxLength),
$"最大长度必须至少为后缀长度 ({suffix.Length})");
}
if (str.Length <= maxLength)
{
return str;
}
return string.Concat(str.AsSpan(0, maxLength - suffix.Length), suffix);
}
/// <summary>
/// 使用指定的分隔符连接字符串集合
/// </summary>
/// <param name="values">要连接的字符串集合</param>
/// <param name="separator">分隔符</param>
/// <returns>连接后的字符串</returns>
/// <exception cref="ArgumentNullException">当 values 或 separator 为 null 时抛出</exception>
/// <example>
/// <code>
/// var words = new[] { "Hello", "World" };
/// var result = words.Join(", "); // "Hello, World"
/// </code>
/// </example>
public static string Join(this IEnumerable<string> values, string separator)
{
if (values is null)
{
throw new ArgumentNullException(nameof(values));
}
if (separator is null)
{
throw new ArgumentNullException(nameof(separator));
}
return string.Join(separator, values);
}
}