编辑
2025-02-06
C# 应用
00
请注意,本文编写于 89 天前,最后修改于 89 天前,其中某些信息可能已经过时。

目录

安装
字符串操作
驼峰命名转换
截断字符串
转换为标题格式
数字操作
数字到单词的转换
序数转换
罗马数字转换
日期和时间操作
相对日期
时间跨度
精确的时间跨度
数量操作
复数化
带格式的数量
枚举操作
集合操作
自然语言连接
带格式的集合连接
本地化
结论

Humanizer 是一个强大的 .NET 库,旨在操作和显示字符串、枚举、日期、时间、时间跨度、数字和数量。它能够将开发人员编写的机器友好的数据转换为人类友好的格式,从而提高代码的可读性和用户体验。本文将详细介绍 Humanizer 的使用方法,并提供多个实用的例子。

安装

首先,通过 NuGet 包管理器安装 Humanizer:

C#
Install-Package Humanizer

字符串操作

驼峰命名转换

C#
using Humanizer; class Program { static void Main(string[] args) { string pascalCase = "ThisIsAPascalCaseString"; Console.WriteLine(pascalCase.Humanize()); string camelCase = "thisIsACamelCaseString"; Console.WriteLine(camelCase.Humanize()); string underscored = "this_is_an_underscored_string"; Console.WriteLine(underscored.Humanize()); } }

image.png

截断字符串

C#
string longText = "This is a very long text that needs to be truncated"; Console.WriteLine(longText.Truncate(20, "..."));

image.png

转换为标题格式

C#
string title = "this is a title"; Console.WriteLine(title.Titleize());

image.png

数字操作

数字到单词的转换

C#
Console.WriteLine(1.ToWords()); Console.WriteLine(10.ToWords()); Console.WriteLine(123.ToWords());

image.png

序数转换

C#
Console.WriteLine(1.Ordinalize()); Console.WriteLine(2.Ordinalize()); Console.WriteLine(3.Ordinalize()); Console.WriteLine(4.Ordinalize());

image.png

罗马数字转换

C#
Console.WriteLine(1.ToRoman()); Console.WriteLine(5.ToRoman()); Console.WriteLine(10.ToRoman()); Console.WriteLine(50.ToRoman()); Console.WriteLine(100.ToRoman()); Console.WriteLine(500.ToRoman()); Console.WriteLine(1000.ToRoman());

image.png

日期和时间操作

相对日期

C#
DateTime pastDate = DateTime.Now.AddDays(-1); Console.WriteLine(pastDate.Humanize()); DateTime futureDate = DateTime.Now.AddDays(1); Console.WriteLine(futureDate.Humanize()); DateTime furtherFutureDate = DateTime.Now.AddDays(10); Console.WriteLine(furtherFutureDate.Humanize());

image.png

切换中文

C#
// 设置当前线程的文化信息为中文(中国) Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");

image.png

时间跨度

C#
TimeSpan timeSpan = TimeSpan.FromSeconds(1); Console.WriteLine(timeSpan.Humanize()); timeSpan = TimeSpan.FromSeconds(3615); Console.WriteLine(timeSpan.Humanize()); timeSpan = TimeSpan.FromDays(23); Console.WriteLine(timeSpan.Humanize());

image.png

精确的时间跨度

C#
TimeSpan preciseTimeSpan = TimeSpan.FromSeconds(1.2); Console.WriteLine(preciseTimeSpan.Humanize(precision: 3)); preciseTimeSpan = TimeSpan.FromSeconds(3615.5); Console.WriteLine(preciseTimeSpan.Humanize(precision: 2));

image.png

数量操作

复数化

C#
Console.WriteLine("man".ToQuantity(1)); Console.WriteLine("man".ToQuantity(2)); Console.WriteLine("person".ToQuantity(1)); Console.WriteLine("person".ToQuantity(2));

image.png

带格式的数量

C#
// 设置当前线程的文化信息为中文(中国) Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN"); Console.WriteLine("bottle".ToQuantity(1055, "N0")); Console.WriteLine("bottle".ToQuantity(1, ShowQuantityAs.Words)); Console.WriteLine("bottle".ToQuantity(2, ShowQuantityAs.Words));

image.png

枚举操作

假设我们有以下枚举:

C#
public enum CustomerStatus { NewCustomer, ActiveCustomer, InactiveCustomer, VipCustomer }

我们可以使用 Humanizer 来美化枚举的显示:

C#
Console.WriteLine(CustomerStatus.NewCustomer.Humanize()); Console.WriteLine(CustomerStatus.VipCustomer.Humanize()); // 使用 LetterCasing 参数 Console.WriteLine(CustomerStatus.ActiveCustomer.Humanize(LetterCasing.AllCaps)); Console.WriteLine(CustomerStatus.InactiveCustomer.Humanize(LetterCasing.LowerCase));

image.png

集合操作

自然语言连接

C#
var fruits = new[] { "apple", "banana", "orange" }; Console.WriteLine(fruits.Humanize()); var moreFruits = new[] { "apple", "banana", "orange", "pear", "grape" }; Console.WriteLine(moreFruits.Humanize());

image.png

带格式的集合连接

C#
var numbers = new[] { 1, 2, 3, 4, 5 }; Console.WriteLine(numbers.Humanize(x => x.ToString("N2")));

image.png

本地化

Humanizer 支持多种语言的本地化。以下是一个西班牙语的例子:

C#
using System.Globalization; // 设置当前线程的文化信息为西班牙语 Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-ES"); Console.WriteLine(DateTime.UtcNow.AddHours(-24).Humanize()); // 输出: ayer Console.WriteLine(TimeSpan.FromDays(1).Humanize()); // 输出: 1 día Console.WriteLine("book".ToQuantity(5)); // 输出: 5 libros

结论

Humanizer 是一个功能丰富的库,可以大大提高代码的可读性和用户体验。通过本文提供的示例,你应该能够处理大多数需要"人性化"的场景,包括字符串操作、数字转换、日期时间处理、数量表示、枚举美化和集合操作等。

在开发面向用户的应用程序时,使用 Humanizer 可以让你的输出更加友好和直观。无论是在日志记录、用户界面显示还是报告生成中,Humanizer 都能帮助你轻松地将机器友好的数据转换为人类友好的格式。

本文作者:rick

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!