.NET 帮助

Humanizer C#(它是如何为开发人员工作的)

乔尔迪·巴尔迪亚
乔尔迪·巴尔迪亚
2024年七月1日
分享:

Humanizer 是一个强大而灵活的 .NET 库,可以简化数据处理过程并使之人性化,尤其是在以用户友好的格式显示信息方面。 无论您是否需要将日期转换为相对时间字符串("3天前")要将单词复数化、将数字格式化为单词,或处理枚举、显示字符串、将 Pascal 大小写输入字符串转换为带有自定义描述的句子、将下划线输入字符串转换为正常标题大小写字符串以及长文本截断,Humanizer 提供了大量工具和扩展方法,可在 C#.NET 中优雅地处理这些任务,将非人性化的输入字符串转换为句子。

本文将讨论 C# 中 Humanizer 的详细教程。 我们还将讨论如何使用 Humanizer 和 IronPDF for C# PDF Library 生成 PDF 文档。

在C#中设置Humanizer

要开始使用 Humanizer,您需要通过 NuGet 安装该库。 在您的项目中,您可以通过软件包管理器控制台执行以下命令:

Install-Package Humanizer
Install-Package Humanizer

另外,如果您使用的是 .NET Core CLI,也可以使用以下方法添加 Humanizer:

dotnet add package Humanizer

安装完成后,您可以在 C# 文件中包含相应的命名空间,开始使用 Humanizer:

using Humanizer;
using Humanizer;

日期和时间的人性化

Humanizer 最常见的用途之一是使用 "人性化 "方法将日期和时间转换为人类可读的格式、时间跨度、数字和数量。 这对于显示相对时间尤其有用,例如 "2 小时前 "或 "5 天后"。

实例:人性化的相对时间

DateTime pastDate = DateTime.Now.AddDays(-3);
string humanizedTime = pastDate.Humanize(); // Output: "3 days ago"
DateTime futureDate = DateTime.Now.AddHours(5);
string futureHumanizedTime = futureDate.Humanize(); // Output: "in 5 hours"
DateTime pastDate = DateTime.Now.AddDays(-3);
string humanizedTime = pastDate.Humanize(); // Output: "3 days ago"
DateTime futureDate = DateTime.Now.AddHours(5);
string futureHumanizedTime = futureDate.Humanize(); // Output: "in 5 hours"

Humanizer 扩展方法可以自动处理不同的时间单位,甚至可以调整语法的正确性。

Humanizer C#(如何为开发人员工作):图 1 - 人性化的相对时间输出

人性化时间跨度

Humanizer 还可以对 TimeSpan 对象进行人性化处理,使其易于以可读格式显示持续时间。

实例:人性化时间跨度

TimeSpan timeSpan = TimeSpan.FromMinutes(123);
string humanizedTimeSpan = timeSpan.Humanize(2); // Output: "2 hours, 3 minutes"
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
string humanizedTimeSpan = timeSpan.Humanize(2); // Output: "2 hours, 3 minutes"

Humanizer C#(如何为开发人员工作):图 2 - 人性化时间跨度输出

使用数字

Humanizer 提供了多种方法,可将数字转换为人类可读的单词并处理序数。

示例:将数字转换为文字

int number = 123;
string words = number.ToWords(); // Output: "one hundred and twenty-three"
int number = 123;
string words = number.ToWords(); // Output: "one hundred and twenty-three"

Humanizer C#(如何为开发人员工作):图 3 - 数字到单词的输出

示例:将数字转换为序数

int number = 21;
string ordinal = number.ToOrdinalWords(); // Output: "twenty-first"
int number = 21;
string ordinal = number.ToOrdinalWords(); // Output: "twenty-first"

Humanizer C#(如何为开发人员工作):图 4 - 将数字转换为序数输出

复数化和单数化

Humanizer 可以轻松地在单复数形式之间转换单词,这对于根据数量动态生成长文本非常有用。

示例:单词的复数化和单数化

string singular = "car";
string plural = singular.Pluralize(); // Output: "cars"
string word = "people";
string singularForm = word.Singularize(); // Output: "person"
string singular = "car";
string plural = singular.Pluralize(); // Output: "cars"
string word = "people";
string singularForm = word.Singularize(); // Output: "person"

Humanizer 还能处理不规则的复数化和单数化,使其在各种用例中都能发挥强大的作用。

Humanizer C#(如何为开发人员工作):图 5 - 复数化和单数化输出

枚举的格式化

在 C# 应用程序中经常使用枚举来表示一组已命名的常量。 Humanizer 可以将枚举值转换为人类可读的字符串。

示例:人性化枚举

MyEnum enumValue = MyEnum.FirstValue;
string humanizedEnum = enumValue.Humanize();
System.Console.WriteLine(humanizedEnum);
public enum MyEnum
{
    FirstValue,
    SecondValue
} // Output: "First value"
MyEnum enumValue = MyEnum.FirstValue;
string humanizedEnum = enumValue.Humanize();
System.Console.WriteLine(humanizedEnum);
public enum MyEnum
{
    FirstValue,
    SecondValue
} // Output: "First value"

这种方法尤其适用于在用户界面中显示用户友好的标签。

Humanizer C#(如何为开发人员工作):图 6 - 人性化枚举输出

人性化的字节大小

Humanizer 的另一项便捷功能是将字节大小人性化,将大字节值转换为 KB、MB 或 GB 等可读格式。

示例:人性化字节大小

long bytes = 1048576;
string humanizedBytes = bytes.Bytes().Humanize(); // Output: "1 MB"
long bytes = 1048576;
string humanizedBytes = bytes.Bytes().Humanize(); // Output: "1 MB"

Humanizer C#(如何为开发人员工作):图 7 - 人性化字节大小输出

高级场景

Humanizer 并不局限于上述基本场景。 它支持多种高级功能,如 "截断 "方法以及多种语言和扩展功能。

示例:日期时间偏移的人性化

Humanizer 还可以处理 "DateTimeOffset",这对于处理时区的应用程序非常有用。

DateTimeOffset dateTimeOffset = DateTimeOffset.Now.AddDays(-2);
string humanizedDateTimeOffset = dateTimeOffset.Humanize(); // Output: "2 days ago"
DateTimeOffset dateTimeOffset = DateTimeOffset.Now.AddDays(-2);
string humanizedDateTimeOffset = dateTimeOffset.Humanize(); // Output: "2 days ago"

Humanizer C#(如何为开发人员工作):图 8 - 日期时间偏移输出人性化

性能考虑事项

Humanizer 的设计非常高效,但与其他库一样,其性能取决于使用方式。 对于需要高性能的应用程序,尤其是处理大型数据集或实时处理的应用程序,必须考虑频繁的人性化操作所带来的影响。

IronPDF for C

IronPDF 是适用于 .NET 应用程序的综合性 PDF 生成和处理库。 它使开发人员能够轻松创建、阅读、编辑和提取 PDF 文件中的内容。 IronPDF 设计为用户友好型,提供广泛的功能,包括将 HTML 转换为 PDF、合并文档、添加水印等。 其通用性和强大的功能使其成为在 C# 项目中处理 PDF 文档的绝佳选择。

通过NuGet包管理器安装IronPDF

请按照以下步骤使用 NuGet 软件包管理器安装 IronPDF:

  1. 在 Visual Studio 中打开您的项目

    • 启动 Visual Studio 并打开您现有的 C# 项目或创建一个新项目。
  2. 打开 NuGet 软件包管理器

    • 右键单击解决方案资源管理器中的项目。

    • 从上下文菜单中选择 "管理 NuGet 包..."。

    Humanizer C#(如何为开发人员工作):图 9 - NuGet 软件包管理器

  3. 安装 IronPDF

    • 在 NuGet 包管理器中,转到 "浏览 "选项卡。

    • 搜索IronPDF

    • 从搜索结果中选择 IronPDF 软件包。

    • 点击 "安装 "按钮,将 IronPDF 添加到您的项目中。

    Humanizer C#(如何为开发人员工作):图 10 - IronPDF

    按照这些步骤,IronPDF 将安装完毕,并准备好在您的 C# 项目中使用,让您可以利用其强大的 PDF 操作功能。

C# Humanizer 和 IronPDF 代码示例

using Humanizer;
using IronPdf;
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Instantiate Renderer
        var renderer = new ChromePdfRenderer();
        List<string> content = GenerateHumanizedContent();
        string htmlContent = "<h1>Humanizer Examples</h1><ul>";

        // Iterate over each item in the List and add it to the HTML string
        foreach (var item in content)
        {
            htmlContent += $"<li>{item}</li>";
        }
        htmlContent += "</ul>";

        // Create a PDF from an HTML string using C#
        var pdf = renderer.RenderHtmlAsPdf(htmlContent);

        // Export to a file or stream
        pdf.SaveAs("output.pdf");
    }

    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 Program
{
    static void Main()
    {
        // Instantiate Renderer
        var renderer = new ChromePdfRenderer();
        List<string> content = GenerateHumanizedContent();
        string htmlContent = "<h1>Humanizer Examples</h1><ul>";

        // Iterate over each item in the List and add it to the HTML string
        foreach (var item in content)
        {
            htmlContent += $"<li>{item}</li>";
        }
        htmlContent += "</ul>";

        // Create a PDF from an HTML string using C#
        var pdf = renderer.RenderHtmlAsPdf(htmlContent);

        // Export to a file or stream
        pdf.SaveAs("output.pdf");
    }

    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 C#(如何为开发人员工作):图 11 - PDF 输出

结论

Humanizer 是.NET 开发人员不可或缺的库,其目标是创建以用户友好和人类可读格式呈现信息的应用程序。 从日期和时间人性化到数字和枚举格式化,其广泛的功能使其成为提高应用程序可用性的多功能工具。 利用 Humanizer,开发人员可以节省实施自定义格式化逻辑的时间和精力,确保他们的应用程序能更有效地向最终用户传达数据。

同样,IronPDF 提供全面的 PDF 生成和处理功能,是在 C# 项目中创建和处理 PDF 文档的绝佳选择。 Humanizer 和 IronPDF 可共同显著增强 .NET 应用程序的功能和表现力。 有关 IronPDF 许可的更多详情,请参阅IronPDF 许可证信息. 如需进一步了解,请查看我们的HTML 转换为 PDF 的详细教程.

乔尔迪·巴尔迪亚
乔尔迪·巴尔迪亚
软件工程师
Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 运用技能时,他会进行游戏编程。作为产品测试、产品开发和研究的负责人之一,Jordi 为持续的产品改进增添了极大的价值。多样化的经验让他充满挑战和参与感,他说这是他在 Iron Software 工作中最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。
< 前一页
TensorFlow .NET(它是如何为开发人员工作的)
下一步 >
OpenAPI .NET(开发者如何使用)