namespace GFramework.Core.extensions;
///
/// 字符串扩展方法
///
public static class StringExtensions
{
///
/// 指示指定的字符串是 null 还是空字符串
///
/// 要测试的字符串
/// 如果 str 参数为 null 或空字符串 (""),则为 true;否则为 false
///
///
/// string? text = null;
/// if (text.IsNullOrEmpty()) { /* ... */ }
///
///
public static bool IsNullOrEmpty(this string? str)
{
return string.IsNullOrEmpty(str);
}
///
/// 指示指定的字符串是 null、空还是仅由空白字符组成
///
/// 要测试的字符串
/// 如果 str 参数为 null、空字符串或仅包含空白字符,则为 true;否则为 false
///
///
/// string? text = " ";
/// if (text.IsNullOrWhiteSpace()) { /* ... */ }
///
///
public static bool IsNullOrWhiteSpace(this string? str)
{
return string.IsNullOrWhiteSpace(str);
}
///
/// 如果字符串为空,则返回 null;否则返回原字符串
///
/// 要检查的字符串
/// 如果字符串为空则返回 null,否则返回原字符串
///
///
/// string text = "";
/// var result = text.NullIfEmpty(); // 返回 null
///
///
public static string? NullIfEmpty(this string? str)
{
return string.IsNullOrEmpty(str) ? null : str;
}
///
/// 截断字符串到指定的最大长度,并可选地添加后缀
///
/// 要截断的字符串
/// 最大长度(包括后缀)
/// 截断时添加的后缀,默认为 "..."
/// 截断后的字符串
/// 当 str 为 null 时抛出
/// 当 maxLength 小于后缀长度时抛出
///
///
/// var text = "Hello World";
/// var truncated = text.Truncate(8); // "Hello..."
///
///
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);
}
///
/// 使用指定的分隔符连接字符串集合
///
/// 要连接的字符串集合
/// 分隔符
/// 连接后的字符串
/// 当 values 或 separator 为 null 时抛出
///
///
/// var words = new[] { "Hello", "World" };
/// var result = words.Join(", "); // "Hello, World"
///
///
public static string Join(this IEnumerable values, string separator)
{
ArgumentNullException.ThrowIfNull(values);
ArgumentNullException.ThrowIfNull(separator);
return string.Join(separator, values);
}
}