.NET HELP Humanizer C# (How It Works For Developers) Jacob Mellor 更新:2025年6月22日 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 Humanizer 是一個功能強大且靈活的 .NET 程式庫,它簡化了資料處理過程,使其更加人性化,尤其是以使用者友好的格式顯示資訊。 無論您需要將日期轉換為相對時間字符串("3 天前")、將單詞複數化、將數字格式化為單詞,還是處理枚舉、顯示字符串、將 Pascal 格式的輸入字符串轉換為帶有自定義描述的句子、將帶下劃線的輸入字符串轉換為正常的標題格式字符串以及截斷長文本,Humanizer 都提供了大量的工具和字符串任務,NET 可以處理句子,可以在其中一個優雅地處理句子的人,優雅地處理。 本文將詳細說明 C# 中的 Humanizer 實作方法。 我們也將討論如何使用 Humanizer 和 IronPDF 為 C# PDF 庫產生 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 擴充方法可自動處理不同的時間單位,甚至可以調整文法正確性。 ! C#人性化工具(開發者使用方法):圖1 - 人性化相對時間輸出 時間跨度的人性化 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 ! C# 人性化工具(開發者使用方法):圖 2 - 人性化 TimeSpan 輸出 數位運算 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 ! Humanizer C#(開發者使用方法):圖 3 - 數位轉文字輸出 範例:將數字轉換為序數 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 C#(開發者使用方法):圖 4 - 數字到序數的輸出 複數化和單數化 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 也能處理不規則的複數形式和單數形式,使其能很好地適應各種使用情境。 ! Humanizer C#(開發者使用方法):圖 5 - 輸出複數化與單數化 枚舉格式化 枚舉在 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 這種方法對於在使用者介面中顯示使用者友善的標籤尤其有用。 ! C# 人性化(開發者使用方法):圖 6 - 人性化枚舉輸出 人性化位元組大小 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 ! C# 人性化(開發者使用方法):圖 7 - 人性化位元組大小輸出 進階場景 Humanizer 的功能並不限於上述基本場景。 它支援多種高級功能,例如Truncate方法以及多種語言和擴充。 範例:人性化日期時間偏移量 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 ! 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 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 C#(開發者使用方法):圖 11 - PDF 輸出 結論 Humanizer 是一個不可或缺的程式庫,適用於旨在建立以使用者友好且易於理解的格式呈現資訊的應用程式的 .NET 開發人員。 它具有從日期和時間人性化到數位和枚舉格式等廣泛的功能,使其成為提高應用程式可用性的多功能工具。 透過利用 Humanizer,開發人員可以節省實現自訂格式化邏輯的時間和精力,確保他們的應用程式能夠更有效地向最終用戶傳達資料。 同樣,IronPDF 提供了全面的 PDF 生成和操作功能,使其成為在 C# 專案中建立和處理 PDF 文件的絕佳選擇。 Humanizer 和 IronPDF 結合使用,可顯著增強 .NET 應用程式的功能和呈現效果。 有關 IronPDF 許可的更多詳細信息,請參閱IronPDF 許可資訊。 如需了解更多信息,請查看我們的HTML 轉 PDF 詳細教程。 常見問題解答 C# 中的 Humanizer 函式庫有何用途? 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,然後按一下「安裝」。 將 Humanizer 與 C# 的 PDF 函式庫結合有什麼好處? 透過將 Humanizer 與 IronPDF 等 PDF 函式庫結合,開發人員可以使用 Humanizer 產生人類可讀的內容,然後將其渲染為 PDF 文件,方便建立人性化的 PDF 報告和文件。 使用 Humanizer 時,在效能方面有哪些注意事項? 雖然 Humanizer 的設計目的在於提高效率,但開發人員應考慮在需要高效能的大型資料集或即時處理的應用程式中,頻繁的人性化作業所帶來的影響。 Jacob Mellor 立即與工程團隊聊天 首席技術長 Jacob Mellor 是 Iron Software 的首席技術長,也是開創 C# PDF 技術的有遠見的工程師。作為 Iron Software 核心程式碼庫背後的原始開發人員,他從公司成立之初就塑造了公司的產品架構,與首席執行官 Cameron Rimington 一起將公司轉型為一家 50 多人的公司,為 NASA、Tesla 和全球政府機構提供服務。Jacob 持有曼徹斯特大學土木工程一級榮譽工程學士學位 (BEng)(1998-2001 年)。Jacob 於 1999 年在倫敦開設了他的第一家軟體公司,並於 2005 年創建了他的第一個 .NET 元件,之後,他專門解決微軟生態系統中的複雜問題。他的旗艦產品 IronPDF & Iron Suite for .NET 函式庫在全球的 NuGet 安裝量已超過 3000 萬次,他的基礎程式碼持續為全球使用的開發人員工具提供動力。Jacob 擁有 25 年的商業經驗和 41 年的編碼專業知識,他一直專注於推動企業級 C#、Java 和 Python PDF 技術的創新,同時指導下一代的技術領導者。 相關文章 更新2025年12月11日 Bridging CLI Simplicity & .NET : Using Curl DotNet with IronPDF Jacob Mellor has bridged this gap with CurlDotNet, a library created to bring the familiarity of cURL to the .NET ecosystem. 閱讀更多 更新2025年12月20日 RandomNumberGenerator C# Using the RandomNumberGenerator C# class can help take your PDF generation and editing projects to the next level 閱讀更多 更新2025年12月20日 C# String Equals (How it Works for Developers) When combined with a powerful PDF library like IronPDF, switch pattern matching allows you to build smarter, cleaner logic for document processing 閱讀更多 TensorFlow .NET (How It Works For Developers)OpenAPI .NET (How It Works For Deve...
更新2025年12月11日 Bridging CLI Simplicity & .NET : Using Curl DotNet with IronPDF Jacob Mellor has bridged this gap with CurlDotNet, a library created to bring the familiarity of cURL to the .NET ecosystem. 閱讀更多
更新2025年12月20日 RandomNumberGenerator C# Using the RandomNumberGenerator C# class can help take your PDF generation and editing projects to the next level 閱讀更多
更新2025年12月20日 C# String Equals (How it Works for Developers) When combined with a powerful PDF library like IronPDF, switch pattern matching allows you to build smarter, cleaner logic for document processing 閱讀更多