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());
}
}
C#string longText = "This is a very long text that needs to be truncated";
Console.WriteLine(longText.Truncate(20, "..."));
C#string title = "this is a title";
Console.WriteLine(title.Titleize());
C#Console.WriteLine(1.ToWords());
Console.WriteLine(10.ToWords());
Console.WriteLine(123.ToWords());
C#Console.WriteLine(1.Ordinalize());
Console.WriteLine(2.Ordinalize());
Console.WriteLine(3.Ordinalize());
Console.WriteLine(4.Ordinalize());
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());
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());
切换中文
C#// 设置当前线程的文化信息为中文(中国)
Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
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());
C#TimeSpan preciseTimeSpan = TimeSpan.FromSeconds(1.2);
Console.WriteLine(preciseTimeSpan.Humanize(precision: 3));
preciseTimeSpan = TimeSpan.FromSeconds(3615.5);
Console.WriteLine(preciseTimeSpan.Humanize(precision: 2));
C#Console.WriteLine("man".ToQuantity(1));
Console.WriteLine("man".ToQuantity(2));
Console.WriteLine("person".ToQuantity(1));
Console.WriteLine("person".ToQuantity(2));
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));
假设我们有以下枚举:
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));
C#var fruits = new[] { "apple", "banana", "orange" };
Console.WriteLine(fruits.Humanize());
var moreFruits = new[] { "apple", "banana", "orange", "pear", "grape" };
Console.WriteLine(moreFruits.Humanize());
C#var numbers = new[] { 1, 2, 3, 4, 5 };
Console.WriteLine(numbers.Humanize(x => x.ToString("N2")));
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 许可协议。转载请注明出处!