GFramework/GFramework.Core/extensions/StringExtensions.cs
GeWuYou 475f301d9f feat(extensions): 添加多个扩展方法类和对应测试
- 新增 AsyncExtensions 提供异步任务超时、重试、安全执行等功能
- 新增 CollectionExtensions 提供集合遍历、空值检查、过滤等扩展功能
- 新增 GuardExtensions 提供参数验证的 Guard 模式扩展方法
- 新增 NumericExtensions 提供数值范围限制、插值计算等数学扩展功能
- 为所有扩展方法添加完整的单元测试覆盖正常和异常情况
- 包含详细的 XML 文档注释和使用示例代码
2026-02-25 17:28:52 +08:00

107 lines
3.7 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>如果 str 参数为 null 或空字符串 (""),则为 true否则为 false</returns>
/// <example>
/// <code>
/// string? text = null;
/// if (text.IsNullOrEmpty()) { /* ... */ }
/// </code>
/// </example>
public static bool IsNullOrEmpty(this string? str)
{
return string.IsNullOrEmpty(str);
}
/// <summary>
/// 指示指定的字符串是 null、空还是仅由空白字符组成
/// </summary>
/// <param name="str">要测试的字符串</param>
/// <returns>如果 str 参数为 null、空字符串或仅包含空白字符则为 true否则为 false</returns>
/// <example>
/// <code>
/// string? text = " ";
/// if (text.IsNullOrWhiteSpace()) { /* ... */ }
/// </code>
/// </example>
public static bool IsNullOrWhiteSpace(this string? str)
{
return string.IsNullOrWhiteSpace(str);
}
/// <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 = "...")
{
ArgumentNullException.ThrowIfNull(str);
ArgumentNullException.ThrowIfNull(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)
{
ArgumentNullException.ThrowIfNull(values);
ArgumentNullException.ThrowIfNull(separator);
return string.Join(separator, values);
}
}