.NET幫助 Humanizer C#(對於開發者的運行原理) Curtis Chau 更新日期:6月 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將去人性化的輸入字串轉換為句子。 在本文中,我們將討論C#中Humanizer的詳細教程。 我們還將討論如何使用Humanizer和IronPDF來生成C# PDF庫的PDF文檔。 在C#中設置Humanizer 要開始使用Humanizer,您需要通过NuGet安装该库。 在您的項目中,您可以通過以下命令在Package Manager Console中執行此操作: 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 此方法在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); } } 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是用於.NET應用程序的全面PDF生成和操作庫。 它使開發人員能夠輕鬆創建、閱讀、編輯和提取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# 中的目的是什麼? 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 設計上是高效的,但開發人員應考慮在需要高性能的大型數據集或實時處理的應用中頻繁人性化操作的影響。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新日期 9月 4, 2025 RandomNumberGenerator C# 使用RandomNumberGenerator C#類可以幫助將您的PDF生成和編輯項目提升至新水準 閱讀更多 更新日期 9月 4, 2025 C#字符串等於(它如何對開發者起作用) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 更新日期 8月 5, 2025 C#開關模式匹配(對開發者來說是如何工作的) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 TensorFlow .NET(對於開發者的運行原理)OpenAPI .NET(對於開發者的...