.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天前"),將單詞複數化,將數字格式化為單詞,或與 enums 一起使用,顯示字串,將 Pascal 大小寫輸入字串用自訂描述轉換為句子,將加下劃線的輸入字串轉換為正常標題大小寫字串,以及截斷長文本,Humanizer 提供大量的工具和擴展方法來優雅地處理這些任務,將去人性化的輸入字串轉換為句子。 在本文中,我們將討論 Humanizer 在 C# 中的詳細教程。 我們還將討論如何使用 Humanizer 和 IronPDF 生成 PDF 文件,適用於 C# PDF Library。 在 C# 中設置 Humanizer 要開始使用 Humanizer,您需要透過 NuGet 安裝該程式庫。 在您的專案中,您可以透過套件管理器主控台及以下命令來完成此操作: Install-Package Humanizer 或者,如果您使用 .NET Core CLI,則可以使用以下命令添加 Humanizer: dotnet add package Humanizer 安裝後,您可以透過在 C# 文件中包含適當的命名空間,開始使用 Humanizer。 using Humanizer; using Humanizer; $vbLabelText $csharpLabel 將日期和時間人性化 Humanizer 最常見的用途之一是將日期和時間轉換為人類可讀的格式、timespans、數字和數量,使用 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 擴展方法自動處理不同的時間單位,甚至會調整語法正確性。 Timespans 的人性化 Humanizer 還可以將 TimeSpan 對象人性化,使得顯示持續時間更加容易以可讀格式顯示。 示例:人性化 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 也處理不規則的複數化和單數化,使其對於各種用例非常強大。 格式化 Enums Enums 在 C# 應用程式中經常用來表示一組命名常數。 Humanizer 可以將 enum 值轉換為人類可讀的字串。 示例:人性化 Enums 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 此方法對於在 UI 中顯示使用者友好的標籤特別有用。 字節大小的人性化 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 開發者的必備程式庫,旨在創建以人性化和人類可讀格式呈現資訊的應用程式。 其廣泛的功能範圍,從日期和時間人性化到數字和 enums 格式化,使其成為提高應用程式可用性的多功能工具。 通過利用 Humanizer,開發者可以節省實現自訂格式化邏輯的時間和精力,為終端用戶更有效地傳達數據。 同樣,IronPDF 提供全面的 PDF 生成和操作功能,使其成為在 C# 專案中創建和處理 PDF 文件的絕佳選擇。 結合 Humanizer 和 IronPDF 可以顯著增強 .NET 應用程式的功能和展示。 有關 IronPDF 授權的更多詳細資訊,請參閱IronPDF 授權資訊。 如需進一步探索,請查看我們的HTML 到 PDF 轉換的詳細教程。 常見問題解答 Humanizer 庫在 C# 中的目的是什麼? Humanizer 庫在 C# 中設計用於將數據轉換為人類友好的格式,例如將日期轉換為相對時間字符串,將單詞複數化,將數字格式化為單詞,並處理枚舉。它可以幫助開發人員以更易讀和可訪問的方式呈現數據。 如何在 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,然後點擊“安裝”。 將 Humanizer 與 C# 中的 PDF 庫結合使用有哪些好處? 通過將 Humanizer 與 IronPDF 這樣的 PDF 庫結合使用,開發人員可以使用 Humanizer 生成人類可讀的內容,然後將其渲染到 PDF 文檔中,從而促進生成用戶友好的 PDF 報告和文檔。 使用 Humanizer 時應考慮哪些性能方面的問題? 雖然 Humanizer 設計上是高效的,但開發人員應考慮在需要高性能的大型數據集或實時處理的應用中頻繁人性化操作的影響。 Jacob Mellor 立即與工程團隊聊天 首席技術官 Jacob Mellor是Iron Software的首席技術官,也是開創C# PDF技術的前瞻性工程師。作為Iron Software核心代碼庫的原始開發者,他自公司成立以來就塑造了公司的產品架構,並與CEO Cameron Rimington將公司轉型為服務NASA、Tesla以及全球政府機構的50多人公司。Jacob擁有曼徹斯特大學土木工程一級榮譽學士學位(1998年–2001年)。他於1999年在倫敦開立首家軟體公司,並於2005年建立了他的第一個.NET組件,專注於解決Microsoft生態系統中的複雜問題。他的旗艦作品IronPDF和Iron Suite .NET程式庫全球已獲得超過3000萬次NuGet安裝,他的基礎代碼不斷在全球各地驅動開發者工具。擁有25年以上的商業經驗和41年的編碼專業知識,Jacob仍然專注於推動企業級C#、Java和Python PDF技術的創新,同時指導下一代技術領導者。 相關文章 更新2026年2月20日 銜接 CLI 簡化與 .NET : 使用 Curl DotNet 與 IronPDF for .NET Jacob Mellor 藉由 CurlDotNet 彌補了這方面的不足,CurlDotNet 是為了讓 .NET 生態系統能熟悉 cURL 而建立的函式庫。 閱讀更多 更新2025年12月20日 RandomNumberGenerator C# 使用RandomNumberGenerator C#類可以幫助將您的PDF生成和編輯項目提升至新水準 閱讀更多 更新2025年12月20日 C#字符串等於(它如何對開發者起作用) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 TensorFlow .NET(對於開發者的運行原理)OpenAPI .NET(對於開發者的...
更新2026年2月20日 銜接 CLI 簡化與 .NET : 使用 Curl DotNet 與 IronPDF for .NET Jacob Mellor 藉由 CurlDotNet 彌補了這方面的不足,CurlDotNet 是為了讓 .NET 生態系統能熟悉 cURL 而建立的函式庫。 閱讀更多