.NET 帮助 Humanizer C#(开发人员如何使用) Jacob Mellor 已更新:2026年1月18日 下载 IronPDF NuGet 下载 DLL 下载 Windows 安装程序 免费试用 LLM副本 LLM副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 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; $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); } } $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); } } $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); } } $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); } } $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); } } $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); } } $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); } } $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); } } $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; } } $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被设计为高效的,开发人员还是应该考虑在需要高性能的大型数据集或实时处理中频繁进行人性化操作的影响。 Jacob Mellor 立即与工程团队聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技术官,也是一位开创 C# PDF 技术的有远见的工程师。作为 Iron Software 核心代码库的原始开发者,他从公司成立之初就开始塑造公司的产品架构,与首席执行官 Cameron Rimington 一起将公司转变为一家拥有 50 多名员工的公司,为 NASA、特斯拉和全球政府机构提供服务。Jacob 拥有曼彻斯特大学土木工程一级荣誉工程学士学位(BEng)(1998-2001 年)。他的旗舰产品 IronPDF 和 Iron Suite for .NET 库在全球的 NuGet 安装量已超过 3000 万次,其基础代码继续为全球使用的开发人员工具提供动力。Jacob 拥有 25 年的商业经验和 41 年的编码专业知识,他一直专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。 相关文章 已更新2026年2月20日 架起 CLI 简洁性与 .NET 的桥梁:使用 IronPDF for .NET 的 Curl DotNet Jacob Mellor 通过 CurlDotNet 填补了这一空白,CurlDotNet 库的创建是为了将 cURL 的熟悉感带入 .NET 生态系统。 阅读更多 已更新2025年12月20日 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多 已更新2025年12月20日 C# String Equals(开发者用法) 与强大的 PDF 库 IronPDF 结合使用,切换模式匹配允许您为文档处理构建更智能、更简洁的逻辑。 阅读更多 TensorFlow .NET(开发人员如何使用)OpenAPI .NET(对开发人员的...
已更新2026年2月20日 架起 CLI 简洁性与 .NET 的桥梁:使用 IronPDF for .NET 的 Curl DotNet Jacob Mellor 通过 CurlDotNet 填补了这一空白,CurlDotNet 库的创建是为了将 cURL 的熟悉感带入 .NET 生态系统。 阅读更多
已更新2025年12月20日 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多