C# Humanizer
最后修改于 2023 年 7 月 6 日
在本文中,我们使用 Humanizer.Core 库来人性化字符串、日期时间、数字和数量。人性化是指使信息更容易被人理解。
$ dotnet add package Humanizer.Core
我们将库添加到我们的项目中。
转换文本
Transform
方法使用提供的转换器转换字符串。
Program.cs
using Humanizer; Console.OutputEncoding = System.Text.Encoding.UTF8; var msg = "an old falcon in the sky"; Console.WriteLine(msg.Transform(To.LowerCase)); Console.WriteLine(msg.Transform(To.SentenceCase)); Console.WriteLine(msg.Transform(To.TitleCase)); Console.WriteLine(msg.Transform(To.UpperCase)); var msg2 = "старый сокол в небе"; Console.WriteLine(msg2.Transform(To.LowerCase)); Console.WriteLine(msg2.Transform(To.SentenceCase)); Console.WriteLine(msg2.Transform(To.TitleCase)); Console.WriteLine(msg2.Transform(To.UpperCase));
在程序中,我们转换字符串的大小写。
$ dotnet run an old falcon in the sky An old falcon in the sky An Old Falcon in the Sky AN OLD FALCON IN THE SKY старый сокол в небе Старый сокол в небе Старый Сокол В Небе СТАРЫЙ СОКОЛ В НЕБЕ
截断字符串
当我们在显示中无法显示整个字符串时,通常的做法是用截断后缀(通常是三个点)来缩短字符串。
Program.cs
using Humanizer; var msg = "an old falcon in the sky"; Console.WriteLine(msg.Truncate(15)); Console.WriteLine(msg.Truncate(15, Truncator.FixedLength, TruncateFrom.Left)); Console.WriteLine(msg.Truncate(15, "...", TruncateFrom.Left)); Console.WriteLine(msg.Truncate(15, "...")); Console.WriteLine(msg.Truncate(4, "...", Truncator.FixedNumberOfWords)); Console.WriteLine(msg.Truncate(15, "...", Truncator.FixedNumberOfCharacters));
我们使用 Truncate
方法来缩短字符串。 我们可以基于字符或单词缩短字符串。 字符串可以从右侧或左侧缩短。
$ dotnet run an old falcon … …con in the sky ...n in the sky an old falco... an old falcon in... an old falcon i...
罗马数字
我们可以将数字从阿拉伯数字转换为罗马数字。
Program.cs
using Humanizer; int[] vals = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 20, 100, 125, 500, 1000, 1555 }; var nromans = new List<string>(); var narabic = new List<int>(); foreach (var e in vals) { nromans.Add(e.ToRoman()); } var rliterals = new string[] { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XX", "C", "CXXV", "D", "M", "MDLV" }; foreach (var e in rliterals) { narabic.Add(e.FromRoman()); } Console.WriteLine(string.Join(",", nromans)); Console.WriteLine(string.Join(",", narabic));
我们定义一个数字数组。 我们将它们转换为罗马数字,然后再转换回阿拉伯数字。
nromans.Add(e.ToRoman());
ToRoman
将值转换为罗马数字。
narabic.Add(e.FromRoman());
FromRoman
将罗马数字转换为阿拉伯数字。
$ dotnet run I,II,III,IV,V,VI,VII,VIII,IX,X,XI,XX,C,CXXV,D,M,MDLV 1,2,3,4,5,6,7,8,9,10,11,20,100,125,500,1000,1555
将数字转换为单词
ToWords
方法将数字转换为单词。 ToOrdinalWords
将数字转换为序数词。
Program.cs
using Humanizer; int[] vals = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 20, 100, 125, 500, 1000, 1555 }; foreach (var e in vals) { Console.WriteLine(e.ToWords()); } Console.WriteLine("-------------------------"); foreach (var e in vals) { Console.WriteLine(e.ToOrdinalWords()); }
我们有一个整数值数组。 这些被转换为单词和序数词。
$ dotnet run one two three four five six seven eight nine ten eleven twenty one hundred one hundred and twenty-five five hundred one thousand one thousand five hundred and fifty-five ------------------------- first second third fourth fifth sixth seventh eighth ninth tenth eleventh twentieth hundredth hundred and twenty-fifth five hundredth thousandth thousand five hundred and fifty-fifth
单数化 & 复数化单词
Singularize
方法在考虑不规则单词的情况下将提供的输入单数化,而 Pluralize
方法将其复数化。
Program.cs
using Humanizer; var words = new string[] { "geese", "octopi", "mice", "passers-by", "men", "men-servants", "dormice", "lice", "teeth", "feet" }; foreach (var word in words) { Console.WriteLine(word.Singularize()); } Console.WriteLine("-------------------------"); var words2 = new string[] { "goose", "octopus", "mouse", "passer-by", "man", "man-servant", "dormouse", "louse", "tooth", "foot" }; foreach (var word in words2) { Console.WriteLine(word.Pluralize()); }
我们将一些英语单词单数化和复数化。
$ dotnet run goose octopus mouse passers-by man men-servant dormice louse tooth foot ------------------------- geese octopi mice passer-bies men man-servants dormouses lice teeth feet
请注意,passer-by,man-servant 和 dormouse 单词被错误地屈折变化。
日期和时间
我们可以使用 Humanize
来人性化日期时间值。
Program.cs
using Humanizer; using Humanizer.Localisation; Console.WriteLine(TimeSpan.FromDays(486).Humanize(maxUnit: TimeUnit.Year)); Console.WriteLine(TimeSpan.FromDays(486).Humanize(precision: 3, maxUnit: TimeUnit.Year)); Console.WriteLine(TimeSpan.FromMinutes(1600).Humanize(4)); Console.WriteLine(TimeSpan.FromMinutes(1600).Humanize()); Console.WriteLine(TimeSpan.FromMinutes(1600).Humanize(maxUnit: TimeUnit.Hour)); Console.WriteLine(TimeSpan.FromMilliseconds(5553005).Humanize(3)); Console.WriteLine(DateTime.UtcNow.AddHours(-30).Humanize()); Console.WriteLine(DateTime.UtcNow.AddHours(-2).Humanize()); Console.WriteLine(DateTime.UtcNow.AddHours(30).Humanize()); Console.WriteLine(DateTime.UtcNow.AddHours(2).Humanize());
precision
选项设置要返回的最大时间单位数。 默认值为 1,这意味着返回最大的单位。 maxUnit
设置要输出的最大时间单位。
$ dotnet run 1 year 1 year, 3 months, 29 days 1 day, 2 hours, 40 minutes 1 day 26 hours 1 hour, 32 minutes, 33 seconds yesterday 2 hours ago tomorrow an hour from now
来源
在本文中,我们介绍了 Humanizer.Core
库。
作者
列出所有 C# 教程。