.NET 帮助 Humanizer C#(开发人员如何使用) Curtis Chau 已更新:六月 22, 2025 Download IronPDF NuGet 下载 DLL 下载 Windows 安装程序 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 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; Imports Humanizer $vbLabelText $csharpLabel 人性化日期和时间 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); } } Imports System Friend Class HumanizerDemo Shared Sub Main() Dim pastDate As DateTime = DateTime.Now.AddDays(-3) ' Humanize the past date, which converts it to a relative time format Dim humanizedTime As String = pastDate.Humanize() ' Output: "3 days ago" Dim futureDate As DateTime = DateTime.Now.AddHours(5) ' Humanize the future date, presenting it in relative time Dim futureHumanizedTime As String = futureDate.Humanize() ' Output: "in 5 hours" Console.WriteLine("Humanized Past Date: " & humanizedTime) Console.WriteLine("Humanized Future Date: " & futureHumanizedTime) End Sub End Class $vbLabelText $csharpLabel 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); } } Imports System Friend Class TimeSpanHumanizerDemo Shared Sub Main() Dim timeSpan As TimeSpan = System.TimeSpan.FromMinutes(123) ' Humanizing the TimeSpan into hours and minutes Dim humanizedTimeSpan As String = timeSpan.Humanize(2) ' Output: "2 hours, 3 minutes" Console.WriteLine("Humanized TimeSpan: " & humanizedTimeSpan) End Sub End Class $vbLabelText $csharpLabel 处理数字 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); } } Imports System Friend Class NumberHumanizerDemo Shared Sub Main() Dim number As Integer = 123 ' Convert number to words Dim words As String = number.ToWords() ' Output: "one hundred and twenty-three" Console.WriteLine("Number in Words: " & words) End Sub End Class $vbLabelText $csharpLabel 示例:将数字转换为序数 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); } } Imports System Friend Class OrdinalHumanizerDemo Shared Sub Main() Dim number As Integer = 21 ' Convert number to ordinal words Dim ordinal As String = number.ToOrdinalWords() ' Output: "twenty-first" Console.WriteLine("Ordinal Number: " & ordinal) End Sub End Class $vbLabelText $csharpLabel 复数化和单数化 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); } } Imports System Friend Class PluralizationDemo Shared Sub Main() Dim singular As String = "car" ' Pluralize the word Dim plural As String = singular.Pluralize() ' Output: "cars" Dim word As String = "people" ' Singularize the word Dim singularForm As String = word.Singularize() ' Output: "person" Console.WriteLine("Plural of 'car': " & plural) Console.WriteLine("Singular of 'people': " & singularForm) End Sub End Class $vbLabelText $csharpLabel 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); } } Imports System Public Enum MyEnum FirstValue SecondValue End Enum Friend Class EnumHumanizerDemo Shared Sub Main() Dim enumValue As MyEnum = MyEnum.FirstValue ' Humanizing enum to a readable format Dim humanizedEnum As String = enumValue.Humanize() ' Output: "First value" Console.WriteLine("Humanized Enum: " & humanizedEnum) End Sub End Class $vbLabelText $csharpLabel 这种方法对在用户界面中显示用户友好的标签特别有用。 人性化字节大小 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); } } Imports System Friend Class ByteSizeHumanizerDemo Shared Sub Main() Dim bytes As Long = 1048576 ' Humanize bytes to a readable size format Dim humanizedBytes As String = bytes.Bytes().Humanize() ' Output: "1 MB" Console.WriteLine("Humanized Byte Size: " & humanizedBytes) End Sub End Class $vbLabelText $csharpLabel 高级场景 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); } } Imports System Friend Class DateTimeOffsetHumanizerDemo Shared Sub Main() Dim dateTimeOffset As DateTimeOffset = System.DateTimeOffset.Now.AddDays(-2) ' Humanize DateTimeOffset Dim humanizedDateTimeOffset As String = dateTimeOffset.Humanize() ' Output: "2 days ago" Console.WriteLine("Humanized DateTimeOffset: " & humanizedDateTimeOffset) End Sub End Class $vbLabelText $csharpLabel 性能考虑 Humanizer 旨在提高效率,但与任何库一样,其性能取决于使用方式。 对于需要高性能的应用程序,特别是那些处理大数据集或实时处理的应用程序,考虑频繁的人性化操作的影响至关重要。 IronPDF for C IronPDF 是一个全面的 PDF 生成和操控库,适用于 .NET 应用程序。 它使开发人员能够轻松创建、读取、编辑和提取 PDF 文件的内容。 IronPDF 旨在对用户友好,并提供一系列广泛的功能,包括将 HTML 转换为 PDF、合并文档、添加水印等。 其多功能性和强大的功能使其成为在 C# 项目中处理 PDF 文档的优秀选择。 通过 NuGet 包管理器安装 IronPDF 按照以下步骤,通过 NuGet 包管理器安装 IronPDF: 在 Visual Studio 中打开您的项目: 启动 Visual Studio 并打开现有的 C# 项目或创建一个新项目。 打开 NuGet 包管理器: 在解决方案资源管理器中右键单击项目。 从上下文菜单中选择“管理 NuGet 包...” 安装 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; } } Imports Humanizer Imports IronPdf Imports System Imports System.Collections.Generic Friend Class PDFGenerationDemo Shared Sub Main() ' Instantiate the PDF renderer Dim renderer = New ChromePdfRenderer() ' Generate humanized content Dim content As List(Of String) = GenerateHumanizedContent() ' HTML content template for the PDF Dim htmlContent As String = "<h1>Humanizer Examples</h1><ul>" ' Build the list items to add to the HTML content For Each item In content htmlContent &= $"<li>{item}</li>" Next item htmlContent &= "</ul>" ' Render the HTML into a PDF document Dim pdf = renderer.RenderHtmlAsPdf(htmlContent) ' Save the PDF to a file pdf.SaveAs("output.pdf") Console.WriteLine("PDF document generated successfully: output.pdf") End Sub ''' <summary> ''' Generates a list of humanized content examples ''' </summary> ''' <returns>List of humanized content as strings</returns> Private Shared Function GenerateHumanizedContent() As List(Of String) Dim content As New List(Of String)() ' DateTime examples Dim pastDate As DateTime = DateTime.Now.AddDays(-3) Dim futureDate As DateTime = 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 Dim timeSpan As TimeSpan = System.TimeSpan.FromMinutes(123) content.Add($"TimeSpan of 123 minutes: {timeSpan.Humanize()}") ' Number examples Dim number As Integer = 12345 content.Add($"Number 12345 in words: {number.ToWords()}") content.Add($"Ordinal of 21: {21.ToOrdinalWords()}") ' Pluralization examples Dim singular As String = "car" content.Add($"Plural of 'car': {singular.Pluralize()}") Dim plural As String = "children" content.Add($"Singular of 'children': {plural.Singularize()}") ' Byte size examples Dim bytes As Long = 1048576 content.Add($"1,048,576 bytes: {bytes.Bytes().Humanize()}") Return content End Function End Class $vbLabelText $csharpLabel 结论 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被设计为高效的,开发人员还是应该考虑在需要高性能的大型数据集或实时处理中频繁进行人性化操作的影响。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已更新九月 4, 2025 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多 已更新九月 4, 2025 C# String Equals(开发者用法) 与强大的 PDF 库 IronPDF 结合使用,切换模式匹配允许您为文档处理构建更智能、更简洁的逻辑。 阅读更多 已更新八月 5, 2025 C# Switch 模式匹配(开发者用法) 与强大的 PDF 库 IronPDF 结合使用,切换模式匹配允许您为文档处理构建更智能、更简洁的逻辑。 阅读更多 TensorFlow .NET(开发人员如何使用)OpenAPI .NET(开发人员如何...
已更新九月 4, 2025 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多