推荐一个开源.Net库,方便我们操作和格式化:字符串、枚举、日期、时间、时间跨度、数字等。
01
项目简介
Humanizer 是一个功能强大且广受欢迎的开源 .NET 库,其核心目标是将机器友好的数据转换成人类更易读、更自然、更符合语言习惯的格式。
它为 .NET 中常见的数据类型(如字符串、枚举、数字、日期时间、时间间隔等)提供了大量扩展方法,让你的代码输出更加“人性化”,从而提升用户体验和代码的可读性。
简单来说,它让你的应用程序输出的文字看起来更像是自然语言,而不是冰冷的代码格式。
02
支持框架
1、支持.net48、.net6.0、.net7.0 和 ,net8.0;
2、指定语言:英语包Humanizer.Core,您想要的特定语言包。例如,对于法语,请使用Humanizer.Core.fr。您可以安装任意数量的语言包来包含多种语言。
03
使用示例
1、字符串人性化 (String Humanization)
将机器的字符串(如 PascalCase, snake_case)转换为人类可读的句子。
// 将 PascalCase 或 snake_case 转换为带空格的句子
Console.WriteLine(“PascalCaseInput”.Humanize());
// 输出: “Pascal Case Input”
Console.WriteLine(“snake_case_input”.Humanize());
// 输出: “snake case input”
Console.WriteLine(“HTML”.Humanize());
// 输出: “HTML” (全大写被视为缩写,保持不变)
// 如果你想强制转换全大写单词,可以先转换大小写
Console.WriteLine(“HTML”.Transform(To.TitleCase));
// 输出: “Html” (注意:通常我们期望 “HTML” 保持原样)
// 使用 Transform 方法进行大小写转换 (推荐,旧的 LetterCasing 已过时)
Console.WriteLine(“can return title case”.Transform(To.TitleCase));
// 输出: “Can Return Title Case”
Console.WriteLine(“CAN RETURN UPPER CASE”.Transform(To.SentenceCase));
// 输出: “Can return upper case”
- 字符串截断 (String Truncation)
智能地截断长字符串。
string longText = “This is a very long sentence that needs to be shortened.”;
// 截断到指定长度,默认使用 ‘…’ (Unicode 省略号)
Console.WriteLine(longText.Truncate(20));
// 输出: “This is a very long…”
// 使用自定义截断字符串
Console.WriteLine(longText.Truncate(20, “…”));
// 输出: “This is a very long…”
// 从字符串开头截断
Console.WriteLine(longText.Truncate(20, Truncator.FixedLength, TruncateFrom.Left));
// 输出: “…needs to be shortened.”
- 数字人性化 (Number Humanization)
将数字转换为更易读或更自然的格式。
int number = 123456;
// 转换为英文单词
Console.WriteLine(number.ToWords());
// 输出: “one hundred and twenty-three thousand four hundred and fifty-six”
// 转换为序数词
Console.WriteLine(1.Ordinalize()); // 输出: “1st”
Console.WriteLine(2.Ordinalize()); // 输出: “2nd”
Console.WriteLine(21.Ordinalize()); // 输出: “21st”
Console.WriteLine(11.Ordinalize()); // 输出: “11th” (特殊规则)
// 将大数字转换为带单位的简洁形式 (K, M, G)
Console.WriteLine(number.ToMetric());
// 输出: “123.46K”
// 格式化文件大小
Console.WriteLine(1024.ToBits()); // 输出: “1.00 Kb”
Console.WriteLine(1048576.ToBits()); // 输出: “1.00 Mb”
- 日期时间人性化 (DateTime Humanization)
将日期时间转换为相对时间描述,非常适用于显示“刚刚”、“2小时前”、“明天”等。
// 假设现在是 2025-08-20 22:00:00
DateTime now = DateTime.Now; // 2025-08-20 22:00:00
// 15分钟前
DateTime past = now.AddMinutes(-15);
Console.WriteLine(past.Humanize());
// 输出: “15 minutes ago” (根据实际时间差)
// 3小时前
DateTime threeHoursAgo = now.AddHours(-3);
Console.WriteLine(threeHoursAgo.Humanize());
// 输出: “3 hours ago”
// 明天
DateTime tomorrow = now.AddDays(1);
Console.WriteLine(tomorrow.Humanize());
// 输出: “tomorrow”
// 10天后
DateTime tenDaysLater = now.AddDays(10);
Console.WriteLine(tenDaysLater.Humanize());
// 输出: “in 10 days”
// 也可以用于未来的日期
DateTime future = now.AddHours(2);
Console.WriteLine(future.Humanize());
// 输出: “2 hours from now”
- 时间间隔人性化 (TimeSpan Humanization)
将 TimeSpan 对象转换为可读的描述。
TimeSpan duration = TimeSpan.FromHours(2) + TimeSpan.FromMinutes(30) + TimeSpan.FromSeconds(45);
// 默认只显示最大单位
Console.WriteLine(duration.Humanize());
// 输出: “2 hours”
// 指定精度 (显示前几个单位)
Console.WriteLine(duration.Humanize(precision: 2));
// 输出: “2 hours, 30 minutes”
Console.WriteLine(duration.Humanize(precision: 3));
// 输出: “2 hours, 30 minutes, 45 seconds”
// 使用 ‘and’ 连接 (依赖于文化设置或自定义分隔符)
Console.WriteLine(duration.Humanize(precision: 3, collectionSeparator: “, and “));
// 输出: “2 hours, 30 minutes, and 45 seconds”
- 枚举人性化 (Enum Humanization)
将枚举值转换为更友好的字符串。
public enum MovieGenre
{
Action,
ScienceFiction,
[Description(“Romantic Comedy”)] // 使用 DescriptionAttribute 自定义
RomCom
}
// 人性化枚举值
Console.WriteLine(MovieGenre.Action.Humanize());
// 输出: “Action”
Console.WriteLine(MovieGenre.ScienceFiction.Humanize());
// 输出: “Science Fiction” (自动添加空格)
Console.WriteLine(MovieGenre.RomCom.Humanize());
// 输出: “Romantic Comedy” (使用 Description 属性)
// 将人性化字符串转换回枚举 (Dehumanize)
string input = “Science Fiction”;
var genre = input.DehumanizeTo();
Console.WriteLine(genre);
// 输出: ScienceFiction
- 单复数转换 (Pluralization/Singularization)
处理英语名词的单复数。
// 转复数
Console.WriteLine(“apple”.Pluralize());
// 输出: “apples”
Console.WriteLine(“person”.Pluralize());
// 输出: “people” (不规则复数)
Console.WriteLine(“sheep”.Pluralize());
// 输出: “sheep” (单复数同形)
// 转单数
Console.WriteLine(“apples”.Singularize());
// 输出: “apple”
Console.WriteLine(“people”.Singularize());
// 输出: “person”
04
项目地址
https://github.com/Humanizr/Humanizer
声明:来自编程乐趣,仅代表创作者观点。链接:https://eyangzhen.com/2366.html