Humanizer C#(开发人员如何使用)
Humanizer 是一个强大而灵活的 .NET 库,可以简化和人性化数据处理过程,尤其是在以用户友好的格式显示信息时。 无论是需要将日期转换为相对时间字符串("3 天前"),复数化单词,将数字格式化为单词,还是处理枚举、显示字符串、将 Pascal 大小写输入字符串作为句子显示带有自定义描述,底线输入字符串转换为正常标题大小写字符串,和长文本截断,Humanizer 提供了一套丰富的工具和扩展方法,可以在 C#.NET中优雅地处理这些任务,将去人性化的输入字符串转换为句子。
在本文中,我们将讨论 Humanizer 在 C# 中的详细教程。 我们还将讨论如何使用 Humanizer 和 IronPDF 生成 PDF 文档,该文档为 C# PDF 库。
在 C# 中设置 Humanizer
要开始使用 Humanizer,您需要通过 NuGet 安装该库。 在您的项目中,您可以通过包管理器控制台使用以下命令进行安装:
Install-Package Humanizer
或者,如果您使用的是 .NET Core CLI,您可以使用以下命令添加 Humanizer:
dotnet add package Humanizer
安装后,您可以通过在 C# 文件中包含适当的命名空间来开始使用 Humanizer:
using Humanizer;using Humanizer;人性化日期和时间
Humanizer 最常见的用途之一是将日期和时间转换成人类可读的格式,时间间隔、数字和数量,使用 Humanize 方法。 这对于显示相对时间特别有用,例如"2 小时前"或"5 天后"。
示例:人性化相对时间
using System;
class HumanizerDemo
{
static void Main()
{
DateTime pastDate = DateTime.Now.AddDays(-3);
// Humanize the past date, which converts it to a relative time format
string humanizedTime = pastDate.Humanize(); // Output: "3 days ago"
DateTime futureDate = DateTime.Now.AddHours(5);
// Humanize the future date, presenting it in relative time
string futureHumanizedTime = futureDate.Humanize(); // Output: "in 5 hours"
Console.WriteLine("Humanized Past Date: " + humanizedTime);
Console.WriteLine("Humanized Future Date: " + futureHumanizedTime);
}
}using System;
class HumanizerDemo
{
static void Main()
{
DateTime pastDate = DateTime.Now.AddDays(-3);
// Humanize the past date, which converts it to a relative time format
string humanizedTime = pastDate.Humanize(); // Output: "3 days ago"
DateTime futureDate = DateTime.Now.AddHours(5);
// Humanize the future date, presenting it in relative time
string futureHumanizedTime = futureDate.Humanize(); // Output: "in 5 hours"
Console.WriteLine("Humanized Past Date: " + humanizedTime);
Console.WriteLine("Humanized Future Date: " + futureHumanizedTime);
}
}Humanizer 扩展方法会自动处理不同的时间单位,甚至会调整语法正确性。

人性化时间间隔
Humanizer 还可以人性化 TimeSpan 对象,使其能够以可读格式显示持续时间。
示例:人性化时间间隔
using System;
class TimeSpanHumanizerDemo
{
static void Main()
{
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
// Humanizing the TimeSpan into hours and minutes
string humanizedTimeSpan = timeSpan.Humanize(2); // Output: "2 hours, 3 minutes"
Console.WriteLine("Humanized TimeSpan: " + humanizedTimeSpan);
}
}using System;
class TimeSpanHumanizerDemo
{
static void Main()
{
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
// Humanizing the TimeSpan into hours and minutes
string humanizedTimeSpan = timeSpan.Humanize(2); // Output: "2 hours, 3 minutes"
Console.WriteLine("Humanized TimeSpan: " + humanizedTimeSpan);
}
}
处理数字
Humanizer 提供了几种方法,可将数字转换为人类可读的单词,并处理序数。
示例:将数字转换为单词
using System;
class NumberHumanizerDemo
{
static void Main()
{
int number = 123;
// Convert number to words
string words = number.ToWords(); // Output: "one hundred and twenty-three"
Console.WriteLine("Number in Words: " + words);
}
}using System;
class NumberHumanizerDemo
{
static void Main()
{
int number = 123;
// Convert number to words
string words = number.ToWords(); // Output: "one hundred and twenty-three"
Console.WriteLine("Number in Words: " + words);
}
}
示例:将数字转换为序数
using System;
class OrdinalHumanizerDemo
{
static void Main()
{
int number = 21;
// Convert number to ordinal words
string ordinal = number.ToOrdinalWords(); // Output: "twenty-first"
Console.WriteLine("Ordinal Number: " + ordinal);
}
}using System;
class OrdinalHumanizerDemo
{
static void Main()
{
int number = 21;
// Convert number to ordinal words
string ordinal = number.ToOrdinalWords(); // Output: "twenty-first"
Console.WriteLine("Ordinal Number: " + ordinal);
}
}
复数化和单数化
Humanizer 可以轻松地在单数和复数形式之间转换单词,这对于根据数量动态生成长文本很有用。
示例:复数化和单数化单词
using System;
class PluralizationDemo
{
static void Main()
{
string singular = "car";
// Pluralize the word
string plural = singular.Pluralize(); // Output: "cars"
string word = "people";
// Singularize the word
string singularForm = word.Singularize(); // Output: "person"
Console.WriteLine("Plural of 'car': " + plural);
Console.WriteLine("Singular of 'people': " + singularForm);
}
}using System;
class PluralizationDemo
{
static void Main()
{
string singular = "car";
// Pluralize the word
string plural = singular.Pluralize(); // Output: "cars"
string word = "people";
// Singularize the word
string singularForm = word.Singularize(); // Output: "person"
Console.WriteLine("Plural of 'car': " + plural);
Console.WriteLine("Singular of 'people': " + singularForm);
}
}Humanizer 还处理不规则的复数化和单数化,使其对于各种用例都非常强大。

格式化枚举
在 C# 应用程序中,枚举经常用于表示一组命名常量。 Humanizer 可以将枚举值转换为人类可读的字符串。
示例:人性化枚举
using System;
public enum MyEnum
{
FirstValue,
SecondValue
}
class EnumHumanizerDemo
{
static void Main()
{
MyEnum enumValue = MyEnum.FirstValue;
// Humanizing enum to a readable format
string humanizedEnum = enumValue.Humanize(); // Output: "First value"
Console.WriteLine("Humanized Enum: " + humanizedEnum);
}
}using System;
public enum MyEnum
{
FirstValue,
SecondValue
}
class EnumHumanizerDemo
{
static void Main()
{
MyEnum enumValue = MyEnum.FirstValue;
// Humanizing enum to a readable format
string humanizedEnum = enumValue.Humanize(); // Output: "First value"
Console.WriteLine("Humanized Enum: " + humanizedEnum);
}
}这种方法对在用户界面中显示用户友好的标签特别有用。

人性化字节大小
Humanizer 的另一个方便功能是能够人性化字节大小,将大字节值转换为可读格式,如 KB、MB 或 GB。
示例:人性化字节大小
using System;
class ByteSizeHumanizerDemo
{
static void Main()
{
long bytes = 1048576;
// Humanize bytes to a readable size format
string humanizedBytes = bytes.Bytes().Humanize(); // Output: "1 MB"
Console.WriteLine("Humanized Byte Size: " + humanizedBytes);
}
}using System;
class ByteSizeHumanizerDemo
{
static void Main()
{
long bytes = 1048576;
// Humanize bytes to a readable size format
string humanizedBytes = bytes.Bytes().Humanize(); // Output: "1 MB"
Console.WriteLine("Humanized Byte Size: " + humanizedBytes);
}
}
高级场景
Humanizer 不仅限于上述的基本场景。 它支持诸如 Truncate 方法、多个语言和扩展等一系列高级功能。
示例:人性化 DateTime 偏移
Humanizer 还可以处理 DateTimeOffset,这对于处理时区的应用程序非常有用。
using System;
class DateTimeOffsetHumanizerDemo
{
static void Main()
{
DateTimeOffset dateTimeOffset = DateTimeOffset.Now.AddDays(-2);
// Humanize DateTimeOffset
string humanizedDateTimeOffset = dateTimeOffset.Humanize(); // Output: "2 days ago"
Console.WriteLine("Humanized DateTimeOffset: " + humanizedDateTimeOffset);
}
}using System;
class DateTimeOffsetHumanizerDemo
{
static void Main()
{
DateTimeOffset dateTimeOffset = DateTimeOffset.Now.AddDays(-2);
// Humanize DateTimeOffset
string humanizedDateTimeOffset = dateTimeOffset.Humanize(); // Output: "2 days ago"
Console.WriteLine("Humanized DateTimeOffset: " + humanizedDateTimeOffset);
}
}
性能考虑
Humanizer 旨在提高效率,但与任何库一样,其性能取决于使用方式。 对于需要高性能的应用程序,特别是那些处理大数据集或实时处理的应用程序,考虑频繁的人性化操作的影响至关重要。
IronPDF for C
IronPDF 是一个全面的 PDF 生成和操控库,适用于 .NET 应用程序。 它使开发人员能够轻松创建、读取、编辑和提取 PDF 文件的内容。 IronPDF 旨在对用户友好,并提供一系列广泛的功能,包括将 HTML 转换为 PDF、合并文档、添加水印等。 其多功能性和强大的功能使其成为在 C# 项目中处理 PDF 文档的优秀选择。
通过 NuGet 包管理器安装 IronPDF
按照以下步骤,通过 NuGet 包管理器安装 IronPDF:
1.在 Visual Studio 中打开您的项目:
- 启动 Visual Studio 并打开现有的 C# 项目或创建一个新项目。
2.打开 NuGet 包管理器:
- 在解决方案资源管理器中右键单击项目。
- 从上下文菜单中选择"管理 NuGet 包..."

3.安装 IronPDF:
- 在 NuGet 包管理器中,转到"浏览"选项卡。
- 搜索 IronPDF。
- 从搜索结果中选择 IronPDF 包。
- 点击"安装"按钮,将 IronPDF 添加到项目中。

通过执行这些步骤,IronPDF 将被安装并可以在您的 C# 项目中使用,使您可以利用其强大的 PDF 操控能力。
C# Humanizer 和 IronPDF 代码示例
using Humanizer;
using IronPdf;
using System;
using System.Collections.Generic;
class PDFGenerationDemo
{
static void Main()
{
// Instantiate the PDF renderer
var renderer = new ChromePdfRenderer();
// Generate humanized content
List<string> content = GenerateHumanizedContent();
// HTML content template for the PDF
string htmlContent = "<h1>Humanizer Examples</h1><ul>";
// Build the list items to add to the HTML content
foreach (var item in content)
{
htmlContent += $"<li>{item}</li>";
}
htmlContent += "</ul>";
// Render the HTML into a PDF document
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF to a file
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF document generated successfully: output.pdf");
}
/// <summary>
/// Generates a list of humanized content examples
/// </summary>
/// <returns>List of humanized content as strings</returns>
static List<string> GenerateHumanizedContent()
{
List<string> content = new List<string>();
// DateTime examples
DateTime pastDate = DateTime.Now.AddDays(-3);
DateTime futureDate = DateTime.Now.AddHours(5);
content.Add($"DateTime.Now: {DateTime.Now}");
content.Add($"3 days ago: {pastDate.Humanize()}");
content.Add($"In 5 hours: {futureDate.Humanize()}");
// TimeSpan examples
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
content.Add($"TimeSpan of 123 minutes: {timeSpan.Humanize()}");
// Number examples
int number = 12345;
content.Add($"Number 12345 in words: {number.ToWords()}");
content.Add($"Ordinal of 21: {21.ToOrdinalWords()}");
// Pluralization examples
string singular = "car";
content.Add($"Plural of 'car': {singular.Pluralize()}");
string plural = "children";
content.Add($"Singular of 'children': {plural.Singularize()}");
// Byte size examples
long bytes = 1048576;
content.Add($"1,048,576 bytes: {bytes.Bytes().Humanize()}");
return content;
}
}using Humanizer;
using IronPdf;
using System;
using System.Collections.Generic;
class PDFGenerationDemo
{
static void Main()
{
// Instantiate the PDF renderer
var renderer = new ChromePdfRenderer();
// Generate humanized content
List<string> content = GenerateHumanizedContent();
// HTML content template for the PDF
string htmlContent = "<h1>Humanizer Examples</h1><ul>";
// Build the list items to add to the HTML content
foreach (var item in content)
{
htmlContent += $"<li>{item}</li>";
}
htmlContent += "</ul>";
// Render the HTML into a PDF document
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF to a file
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF document generated successfully: output.pdf");
}
/// <summary>
/// Generates a list of humanized content examples
/// </summary>
/// <returns>List of humanized content as strings</returns>
static List<string> GenerateHumanizedContent()
{
List<string> content = new List<string>();
// DateTime examples
DateTime pastDate = DateTime.Now.AddDays(-3);
DateTime futureDate = DateTime.Now.AddHours(5);
content.Add($"DateTime.Now: {DateTime.Now}");
content.Add($"3 days ago: {pastDate.Humanize()}");
content.Add($"In 5 hours: {futureDate.Humanize()}");
// TimeSpan examples
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
content.Add($"TimeSpan of 123 minutes: {timeSpan.Humanize()}");
// Number examples
int number = 12345;
content.Add($"Number 12345 in words: {number.ToWords()}");
content.Add($"Ordinal of 21: {21.ToOrdinalWords()}");
// Pluralization examples
string singular = "car";
content.Add($"Plural of 'car': {singular.Pluralize()}");
string plural = "children";
content.Add($"Singular of 'children': {plural.Singularize()}");
// Byte size examples
long bytes = 1048576;
content.Add($"1,048,576 bytes: {bytes.Bytes().Humanize()}");
return content;
}
}
结论
Humanizer 是面向 .NET 开发者的重要库,旨在创建以用户友好和人类可读形式呈现信息的应用程序。 其从日期和时间人性化到数字和枚举格式化等广泛功能,使其成为提高应用程序可用性的多功能工具。 通过利用 Humanizer,开发人员可以节省时间和精力在实现自定义格式化逻辑上,确保他们的应用程序更有效地向终端用户传达数据。
同样,IronPDF 提供全面的 PDF 生成和操控功能,成为在 C# 项目中创建和处理 PDF 文档的优秀选择。 Humanizer 和 IronPDF 结合使用可以显著增强 .NET 应用程序的功能和呈现效果。 有关 IronPDF 许可的更多详细信息,请查看 IronPDF 许可信息。 要了解更多,请查看我们的HTML 到 PDF 转换详细教程。
常见问题解答
Humanizer库在C#中的目的是什么?
C#中的Humanizer库旨在将数据转换为人类友好的格式,例如将日期转换为相对时间字符串,词语复数化,将数字格式化为文字,以及处理枚举。它帮助开发人员以更具可读性和易访问的方式展示数据。
如何在C#中将DateTime转换为相对时间字符串?
您可以使用Humanizer的Humanize方法将DateTime对象转换为相对时间字符串,例如"3天前"或"5小时后"。
如何在C#项目中安装Humanizer库?
要在C#项目中安装Humanizer库,您可以使用NuGet包管理器控制台并输入命令Install-Package Humanizer,或者使用.NET Core CLI输入dotnet add package Humanizer。
Humanizer可以进行哪些数据转换示例?
Humanizer可以执行多种数据转换,例如将Pascal大小写字符串转换为句子,将下划线字符串转换为标题大小写,以及将长文本截短到指定长度。
Humanizer能帮助C#中的词语复数化吗?
是的,Humanizer提供了复数化和单数化词语的方法,能够有效地处理规则和不规则的形式,比如将“car”转换为“cars”或“people”转换为“person”。
Humanizer如何处理C#中的枚举?
Humanizer可以将枚举值转换为人类可读的字符串,从而更容易显示用户友好的标签。
C# PDF库提供哪些功能?
像IronPDF这样的C# PDF库提供功能如创建、读取、编辑和从PDF文件中提取内容。它还可以将HTML转换为PDF,合并文档,并添加水印。
如何在我的项目中安装C# PDF库?
要安装C# PDF库,您可以使用NuGet包管理器,在“浏览”选项卡中搜索库名称(例如IronPDF),然后单击“安装”。
在C#中结合使用Humanizer和PDF库有什么好处?
通过将Humanizer与像IronPDF这样的PDF库结合使用,开发人员可以使用Humanizer生成人类可读的内容,然后将其渲染为PDF文档,从而方便创建用户友好的PDF报告和文档。
在使用Humanizer时,我应该如何考虑性能问题?
尽管Humanizer被设计为高效的,开发人员还是应该考虑在需要高性能的大型数据集或实时处理中频繁进行人性化操作的影响。








