.NET HELP C# String Equals (How it Works for Developers) Jacob Mellor 更新:2025年12月20日 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 在 C# 應用程式中處理 PDF 文件時,不論是檢查擷取的文字、驗證元資料或有條件地修改文件,比較字串都是一項出乎意料的工作。 C# 中的 string.Equals 方法提供了比較字串物件的精確方法,當與 IronPDF for .NET 結合時,它會成為您 PDF 自動化工具包中的強大工具。 在這篇文章中,我們將探討什麼是 string.Equals、為什麼它在 .NET 中很重要,以及您可以如何有效地將它與 IronPDF 搭配使用,IronPDF 是用於產生和操作的領先 .NET PDF 函式庫。 什麼是 C# 中的 string.Equals? C# 的字串等值方法,在 C# 中稱為 string.Equals,用於比較代表字串或其他相容物件的兩個參數的內容。 您可以傳輸純字串值或在比較過程中會轉換為字串的類型物件。 與等於字串的語法糖的 == 運算符不同,使用 string.Equals 明確地允許您: 指定比較規則(例如大小寫敏感比較或文化不敏感比較)。 避免混淆超載的運算符號。 提高條件邏輯的可讀性。 在 .NET Framework 中,方法簽章通常看起來像這樣: public bool Equals(string value, StringComparison comparisonType) public bool Equals(string value, StringComparison comparisonType) 'INSTANT VB TODO TASK: The following line uses invalid syntax: 'public bool Equals(string value, StringComparison comparisonType) $vbLabelText $csharpLabel 在此,public bool 表示回傳值永遠為真或假,視比較結果而定。 基本語法 string.Equals(str1, str2) string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase) string.Equals(str1, str2) string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase) 'INSTANT VB TODO TASK: The following line uses invalid syntax: 'string.Equals(str1, str2) string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase) $vbLabelText $csharpLabel 此方法需要兩個參數:要比較的兩個字串或字串物件,以及可選擇要執行的比較類型。 回傳值為布林值。 根據比較規則,如果兩個參數相等,該方法將返回 true,否則返回 false。 您可以使用不同的 StringComparison 選項來比較兩個字串,例如: Ordinal (二進位比較,大小寫敏感比較) OrdinalIgnoreCase(不區分大小寫比較) 當前文化 InvariantCultureIgnoreCase 如何使用 IronPdf IronPDF for .NET 是一款功能強大的 .NET PDF 函式庫,可讓您在 PDF 文件中搜尋、抽取和處理文字。 透過使用 string.Equals,您可以 將擷取的字串值與已知值比較。 檢查元資料欄位,例如標題、作者和關鍵字。 有條件地編輯您的 PDF 文件,讓您可以根據字串比較結果加入註解、高亮部分或水印。 讓我們來看看以下的範例,string.Equals 在 IronPDF 的工作流程中被證明是有用的。 範例 1:使用 string.Equals 比較擷取的文字 想像您正在掃描 PDF 發票,並想要驗證其中是否包含特定的公司名稱。 以下是一個範例,說明如何避免常見的陷阱,例如 null reference 異常,以及方法如何強健地檢查相等性。 以下範例將使用此 PDF: !a href="/static-assets/pdf/blog/csharp-string-equals/csharp-string-equals-1.webp">Input PDF using IronPdf; using IronPdf.Editing; class Program { public static void Main(string[] args) { var pdf = new IronPdf.PdfDocument("invoice.pdf"); string extractedText = pdf.ExtractAllText(); var lines = extractedText.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { if (string.Equals(line.Trim(), "Acme Corporation", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("Exact match found: Acme Corporation"); } } } } using IronPdf; using IronPdf.Editing; class Program { public static void Main(string[] args) { var pdf = new IronPdf.PdfDocument("invoice.pdf"); string extractedText = pdf.ExtractAllText(); var lines = extractedText.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { if (string.Equals(line.Trim(), "Acme Corporation", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("Exact match found: Acme Corporation"); } } } } Imports Microsoft.VisualBasic Imports IronPdf Imports IronPdf.Editing Friend Class Program Public Shared Sub Main(ByVal args() As String) Dim pdf = New IronPdf.PdfDocument("invoice.pdf") Dim extractedText As String = pdf.ExtractAllText() Dim lines = extractedText.Split( { ControlChars.Cr, ControlChars.Lf }, StringSplitOptions.RemoveEmptyEntries) For Each line In lines If String.Equals(line.Trim(), "Acme Corporation", StringComparison.OrdinalIgnoreCase) Then Console.WriteLine("Exact match found: Acme Corporation") End If Next line End Sub End Class $vbLabelText $csharpLabel 注意:該方法會返回一個布林值,表示兩個字串物件是否包含相同的值。 為何重要: 正如我們在以下輸出中所看到的,使用 string.Equals 與 StringComparison.OrdinalIgnoreCase 可確保 "ACME CORPORATION" 與 "Acme Corporation" 被視為相等 - 這在 OCR 或文字擷取的情境中非常重要,因為在這些情境中,區分大小寫的差異可能會導致不同的行為。 輸出 !a href="/static-assets/pdf/blog/csharp-string-equals/csharp-string-equals-2.webp">Console Output 範例 2:使用 string.Equals 驗證 PDF 元資料 PDF 檔案通常包含 metadata 欄位,例如標題、作者和主題。 IronPDF 能夠用來讀取這些屬性,方便檢查和比較。 using IronPdf; var pdf = new IronPdf.PdfDocument("invoice.pdf"); string author = pdf.MetaData.Author; if (string.Equals(author, "Iron Software", StringComparison.InvariantCulture)) { Console.WriteLine("Invoice was issued by Iron Software."); } using IronPdf; var pdf = new IronPdf.PdfDocument("invoice.pdf"); string author = pdf.MetaData.Author; if (string.Equals(author, "Iron Software", StringComparison.InvariantCulture)) { Console.WriteLine("Invoice was issued by Iron Software."); } Imports IronPdf Private pdf = New IronPdf.PdfDocument("invoice.pdf") Private author As String = pdf.MetaData.Author If String.Equals(author, "Iron Software", StringComparison.InvariantCulture) Then Console.WriteLine("Invoice was issued by Iron Software.") End If $vbLabelText $csharpLabel 此方法對於以元資料為基礎的驗證可防止處理不是字串或無效檔案的系統特別有用。 輸出 !a href="/static-assets/pdf/blog/csharp-string-equals/csharp-string-equals-3.webp">Console Output 範例 3:根據文字匹配添加水印 您可能想要對包含特定關鍵字的 PDF 文件自動加上水印。 以下是如何做到這一點: using IronPdf; using IronPdf.Editing; License.LicenseKey = "IRONSUITE.WRITERS.21046-907F5E67CC-AHYQW6L-RCHLPMRJMU4G-SET72XAF2JNY-LQK45E5JPLGW-XOLPVBEBLHV7-2LHKZRWUZWMO-5LNIZSPF4BM6-UHUH4R-T4MMJ4MEIYSQEA-DEPLOYMENT.TRIAL-LDG2MK.TRIAL.EXPIRES.16.NOV.2025"; var pdf = new IronPdf.PdfDocument("invoice.pdf"); string content = pdf.ExtractAllText(); var lines = content.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { if (string.Equals(line.Trim(), "DRAFT", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("The document is a draft."); // Corrected HTML and CSS string watermark = @"<div style='color:red; font-size:72px; font-weight:bold;'>DRAFT</div>"; pdf.ApplyWatermark(watermark, rotation: 45, opacity: 70, verticalAlignment: VerticalAlignment.Middle, horizontalAlignment: HorizontalAlignment.Center); pdf.SaveAs("watermarked_invoice.pdf"); } } using IronPdf; using IronPdf.Editing; License.LicenseKey = "IRONSUITE.WRITERS.21046-907F5E67CC-AHYQW6L-RCHLPMRJMU4G-SET72XAF2JNY-LQK45E5JPLGW-XOLPVBEBLHV7-2LHKZRWUZWMO-5LNIZSPF4BM6-UHUH4R-T4MMJ4MEIYSQEA-DEPLOYMENT.TRIAL-LDG2MK.TRIAL.EXPIRES.16.NOV.2025"; var pdf = new IronPdf.PdfDocument("invoice.pdf"); string content = pdf.ExtractAllText(); var lines = content.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { if (string.Equals(line.Trim(), "DRAFT", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("The document is a draft."); // Corrected HTML and CSS string watermark = @"<div style='color:red; font-size:72px; font-weight:bold;'>DRAFT</div>"; pdf.ApplyWatermark(watermark, rotation: 45, opacity: 70, verticalAlignment: VerticalAlignment.Middle, horizontalAlignment: HorizontalAlignment.Center); pdf.SaveAs("watermarked_invoice.pdf"); } } Imports Microsoft.VisualBasic Imports IronPdf Imports IronPdf.Editing License.LicenseKey = "IRONSUITE.WRITERS.21046-907F5E67CC-AHYQW6L-RCHLPMRJMU4G-SET72XAF2JNY-LQK45E5JPLGW-XOLPVBEBLHV7-2LHKZRWUZWMO-5LNIZSPF4BM6-UHUH4R-T4MMJ4MEIYSQEA-DEPLOYMENT.TRIAL-LDG2MK.TRIAL.EXPIRES.16.NOV.2025" Dim pdf = New IronPdf.PdfDocument("invoice.pdf") Dim content As String = pdf.ExtractAllText() Dim lines = content.Split( { ControlChars.Cr, ControlChars.Lf }, StringSplitOptions.RemoveEmptyEntries) For Each line In lines If String.Equals(line.Trim(), "DRAFT", StringComparison.OrdinalIgnoreCase) Then Console.WriteLine("The document is a draft.") ' Corrected HTML and CSS Dim watermark As String = "<div style='color:red; font-size:72px; font-weight:bold;'>DRAFT</div>" pdf.ApplyWatermark(watermark, rotation:= 45, opacity:= 70, verticalAlignment:= VerticalAlignment.Middle, horizontalAlignment:= HorizontalAlignment.Center) pdf.SaveAs("watermarked_invoice.pdf") End If Next line $vbLabelText $csharpLabel 透過使用 string.Equals,我們能夠檢查輸入的 PDF 文件是否包含標示為草稿的文字,如果是,程式就會在文件上套用我們的"DRAFT"水印並儲存。 輸出 !a href="/static-assets/pdf/blog/csharp-string-equals/csharp-string-equals-4.webp">Watermark output 在 IronPDF 中使用 string.Equals 的最佳做法。 請務必指定 StringComparison,這可避免因文化或大小寫不同而產生的錯誤。 比較使用者輸入或擷取的文字時,請使用 Trim() 或 Normalize()。 對於大型文件,請僅抽取必要的頁面或片段,以減少開銷。 將 string.Equals 與 Regex 或 Contains 結合,以獲得混合字串匹配策略。 為什麼 IronPDF 能夠輕而易舉? IronPDF 簡化了開發人員與 PDF 互動的方式。 結合 C# 的 string.Equals,讓您無需模板或第三方工具即可建立動態、智慧型的 PDF 工作流程。 IronPDF 的主要優點: 全文萃取與解析。 方便存取元資料。 使用原生 C# 邏輯進行條件操作。 不需要像 Adobe 或 MS Office 之類的外部依賴。 深入了解 StringComparison 選項。 在 C# 中比較字串時,StringComparison 選項的選擇會大大影響比較的行為和效能。 以下是最常用選項的快速概覽: Ordinal: 執行快速、逐字節的比較,對大小寫和文化不敏感。最適合用於文化不重要的精確匹配。 OrdinalIgnoreCase:與上述相同,但忽略大小寫的差異。 非常適合用於 PDF 文本萃取等可能發生大小寫不一致的情況。 當前文化:考量程式碼執行環境的文化規則。 在比較應尊重當地語言規範的面向使用者文字時非常有用。 考慮到當前的文化,對本地化字串比較有用。 CurrentCultureIgnoreCase:與上述相同,但忽略大小寫。 InvariantCulture 和 InvariantCultureIgnoreCase: 提供一致的跨文化比較,這對全球化應用程式中的字串平等至關重要。 選擇正確的選項可確保您的字串比較行為可預測。 對於透過 IronPdf 擷取的 PDF 文字,OrdinalIgnoreCase 通常是最佳的選擇,可平衡效能與可用性。 大型 PDF 文件的效能考量 有效率地處理大型 PDF 是維持回應式應用程式的關鍵。 以下是一些提示: 從特定頁面擷取文字,而非整個檔案。 多次處理相同文件時,可快取擷取的文字。 使用 string.Equals 進行精確的相等檢查,並使用 Contains 進行子串匹配,盡可能避免昂貴的 regex 操作。 利用異步處理或平行化來同時處理多個 PDF。 在比較前先清潔並規範化字串 (字串 str1、字串 str2),以避免隱形字元或格式化所造成的錯誤錯配。 實施這些技術有助於您的 PDF 工作流程在保持比較準確的同時,還能優雅地進行擴充。 錯誤處理與除錯技巧 要避免執行時的問題,並確保可靠性: 防範 null 值,在比較之前檢查物件 obj 或字串是否為 null,以防止 null 參照異常。 記錄擷取的內容,以驗證方法在執行時的回傳內容。 使用 Trim() 和規範化處理尾端空格和特殊的 Unicode 字元。 包括圍繞 PDF 載入和文字萃取的異常處理。 撰寫涵蓋各種輸入情況的單元測試,包括區分大小寫與不區分大小寫的比較。 最後的想法 C# string.Equals 是一種簡單但功能強大的方法,當與 IronPDF 的強大 PDF 功能一起使用時,它會變得更加有效。 無論您是驗證元資料、驗證擷取的文字,或是套用條件邏輯,這個組合都能讓您在 .NET 中完全控制 PDF 自動化工作流程。 🔗 立即開始使用 IronPdf 準備好開始使用 C# 建立智慧型 PDF 工作流程了嗎? 下載 IronPDF NuGet 套件,探索 .NET PDF 生成、編輯和提取的全部功能。 👉免費試用 IronPDF 。 常見問題解答 C# 中的 string.Equals 方法有何作用? C# 中的 string.Equals 方法用於比較字串物件是否相等。它提供了一種精確的方式來判斷兩個字串是否具有相同的值,這對於驗證 PDF 文件中的元資料或擷取文字等任務是非常重要的。 IronPDF 如何在 C# 應用程式中加強 string.Equals 的使用? IronPDF 可透過提供強大的 PDF 操作功能來增強 string.Equals 的使用,其中包括從 PDF 文件中提取文字。這可讓開發人員利用 string.Equals 來比較和驗證擷取的文字,作為 PDF 自動化任務的一部分。 為什麼字串比較在 PDF 文件處理中很重要? 字串比較在 PDF 文件處理中非常重要,因為它能讓開發人員檢查擷取的文字、驗證元資料,以及根據特定字串值有條件地修改文件,確保資料的正確性以及符合預期的內容。 IronPDF 可以處理 PDF 中的元資料驗證嗎? 是的,IronPDF 可以處理 PDF 中的元資料驗證,它允許開發人員提取元資料,並使用 string.Equals 等方法來比較和驗證元資料是否符合指定的條件。 string.Equals 可能會與 IronPDF 一起使用的常見工作有哪些? string.Equals 可能與 IronPDF 一起使用的常見任務包括檢查擷取的文字是否符合條件、驗證 PDF 文件的元資料,以及在進一步處理之前確保特定字串出現在文件中。 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年8月5日 C# Switch Pattern Matching (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 閱讀更多 RandomNumberGenerator C#C# Switch Pattern Matching (How it ...
更新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年8月5日 C# Switch Pattern Matching (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 閱讀更多