跳過到頁腳內容
.NET幫助

C# Concatenate Strings(對於開發者的運行原理)

字串物件和字面值

在 C# 中,字串是 String 類別的物件,該類別提供許多操作字串的方法,包括各種串接方式。 在深入探討串接技術之前,重要的是要了解兩個關鍵概念:字串字面值和字串變數。 字串常量或字串字面值是一個直接插入代碼中的字元序列,如 "Hello",用雙引號括住,通常用於字串格式操作。 相對地,字串變數是儲存在變數中的字串,可以在執行期間動態修改或使用。

使用 + 運算符的基本串接

在 C# 中最簡單的串接字串方法之一是使用+運算符。 此方法很簡單:只需在兩個字串或字串變數之間放一個+,即可完成串接。 這是一個在 C# 程式中基本的範例:

public static void Main() 
{
    string hello = "Hello, ";
    string world = "World!";
    // Concatenate the two strings using the + operator
    string greeting = hello + world;
    Console.WriteLine(greeting);
}
public static void Main() 
{
    string hello = "Hello, ";
    string world = "World!";
    // Concatenate the two strings using the + operator
    string greeting = hello + world;
    Console.WriteLine(greeting);
}
Public Shared Sub Main()
	Dim hello As String = "Hello, "
	Dim world As String = "World!"
	' Concatenate the two strings using the + operator
	Dim greeting As String = hello & world
	Console.WriteLine(greeting)
End Sub
$vbLabelText   $csharpLabel

C# 字串串接(開發者運作方式):圖 1 - 上述 C# 程式中串接字串的控制台輸出: Hello, World!

在此範例中,helloworld變數儲存字串常量。 +運算符用來將這兩個字串合併為一個字串,儲存在greeting變數中。 顯示的結果將是 "Hello, World!"。

使用 String.Concat 方法

在需要串接多個字串的情況下,String.Concat 方法是非常有用的。 此方法可以接受任意數目的字串參數並將它們串接成一個字串。 以下是如何使用此方法的範例:

public static void Main() 
{
    string firstName = "Iron";
    string lastName = "Developer";
    // Concatenate using String.Concat
    string fullName = String.Concat(firstName, " ", lastName);
    Console.WriteLine(fullName);
}
public static void Main() 
{
    string firstName = "Iron";
    string lastName = "Developer";
    // Concatenate using String.Concat
    string fullName = String.Concat(firstName, " ", lastName);
    Console.WriteLine(fullName);
}
Public Shared Sub Main()
	Dim firstName As String = "Iron"
	Dim lastName As String = "Developer"
	' Concatenate using String.Concat
	Dim fullName As String = String.Concat(firstName, " ", lastName)
	Console.WriteLine(fullName)
End Sub
$vbLabelText   $csharpLabel

C# 字串串接(開發者運作方式):圖 2 - 使用 Concat 方法的控制台輸出: Iron Developer

此代碼片段展示如何使用String.Concat來串接三個字串:firstName,一個空格作為字串,以及lastName。 輸出將是 "Iron Developer"。

使用 String.Join 串接字串

String 類別中另一個強大的串接字串方法是String.Join。 此方法不僅可以串接字串,還允許指定一個分隔符放置在每個字串間。 對於使用一致分隔符串接多個字串特別有用:

public static void Main() 
{
    string[] words = { "Hello", "World", "from", "C#" };
    // Use String.Join to join strings with a space as a delimiter
    string sentence = String.Join(" ", words);
    Console.WriteLine(sentence);
}
public static void Main() 
{
    string[] words = { "Hello", "World", "from", "C#" };
    // Use String.Join to join strings with a space as a delimiter
    string sentence = String.Join(" ", words);
    Console.WriteLine(sentence);
}
Public Shared Sub Main()
	Dim words() As String = { "Hello", "World", "from", "C#" }
	' Use String.Join to join strings with a space as a delimiter
	Dim sentence As String = String.Join(" ", words)
	Console.WriteLine(sentence)
End Sub
$vbLabelText   $csharpLabel

C# 字串串接(開發者運作方式):圖 3 - 使用 String.Join 方法的控制台輸出: Hello World from C#

在上述源代碼中,String.Join 接受兩個參數:分隔符 " " 和字串單字的陣列。 它將words的每個元素用空格串接為單個字串,結果輸出為 "Hello World from C#"。

IronPDF 函式庫介紹

C# 字串串接(開發者運作方式):圖 4 - IronPDF for .NET:C# PDF 函式庫

IronPDF 是一個 C# 函式庫,幫助在 .NET 框架中處理 PDF。 它可以以高度精確地從 HTML 使用 IronPDF 產生 PDF,以及使用 CSS、JavaScript 和影像。 IronPDF 使用 Chrome 的呈現引擎,確保您的 PDF 與您正在轉換的 web 內容看起來一模一樣,擁有準確的佈局和設計。 它易於設置,並且跨多個 .NET 應用程序工作,包括 ASP.NET 和 MVC。 您還可以透過添加文字、圖像或以密碼及數位簽名進行安全保護。 IronPDF 可以高效處理重度工作負載,使其適用於需求高的環境。

代碼示例

下述是一個在 C# 中使用 IronPDF 將兩個 HTML 字串串接為一個 PDF 文件的簡單範例。 以下代碼範例假設您已在 .NET 專案中安裝 IronPDF 函式庫。

using IronPdf;

public class PDFGenerator
{
    public static void Main()
    {
        // Set the IronPDF license key
        License.LicenseKey = "License-Key";

        // Create an instance of the ChromePdfRenderer class
        var renderer = new ChromePdfRenderer();

        // Define two HTML strings
        string htmlString1 = "<p>This is the first part of the document.</p>";
        string htmlString2 = "<p>This is the second part of the document.</p>";

        // Concatenate the HTML strings
        string concatenatedHtml = htmlString1 + htmlString2;

        // Generate PDF from the concatenated HTML string
        var pdfDocument = renderer.RenderHtmlAsPdf(concatenatedHtml);

        // Save the PDF to a file
        pdfDocument.SaveAs("ConcatenatedDocument.pdf");
    }
}
using IronPdf;

public class PDFGenerator
{
    public static void Main()
    {
        // Set the IronPDF license key
        License.LicenseKey = "License-Key";

        // Create an instance of the ChromePdfRenderer class
        var renderer = new ChromePdfRenderer();

        // Define two HTML strings
        string htmlString1 = "<p>This is the first part of the document.</p>";
        string htmlString2 = "<p>This is the second part of the document.</p>";

        // Concatenate the HTML strings
        string concatenatedHtml = htmlString1 + htmlString2;

        // Generate PDF from the concatenated HTML string
        var pdfDocument = renderer.RenderHtmlAsPdf(concatenatedHtml);

        // Save the PDF to a file
        pdfDocument.SaveAs("ConcatenatedDocument.pdf");
    }
}
Imports IronPdf

Public Class PDFGenerator
	Public Shared Sub Main()
		' Set the IronPDF license key
		License.LicenseKey = "License-Key"

		' Create an instance of the ChromePdfRenderer class
		Dim renderer = New ChromePdfRenderer()

		' Define two HTML strings
		Dim htmlString1 As String = "<p>This is the first part of the document.</p>"
		Dim htmlString2 As String = "<p>This is the second part of the document.</p>"

		' Concatenate the HTML strings
		Dim concatenatedHtml As String = htmlString1 & htmlString2

		' Generate PDF from the concatenated HTML string
		Dim pdfDocument = renderer.RenderHtmlAsPdf(concatenatedHtml)

		' Save the PDF to a file
		pdfDocument.SaveAs("ConcatenatedDocument.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

C# 字串串接(開發者運作方式):圖 5 - 使用 IronPDF 函式庫將 HTML 字串串接並轉換為 PDF 的 PDF 輸出

這是開始使用 IronPDF 進行 HTML 內容串接並生成 PDF 的基本範例。 您可以透過添加更複雜的 HTML、CSS 進行樣式化和處理更高級的 PDF 功能(如添加頁面或安全設置)來擴展此範例。

結論

C# 字串串接(開發者運作方式):圖 6 - IronPDF 許可證資訊

本教學介紹了 C# 中串接字串的基本方法,依據您的代碼特別需要情況而各自具有實用性。 我們看了使用+運算符的簡單串接,使用String.Concat方法懸接多個字串,和使用String.Join方法加上分隔符進行字串串接。 了解這些技術對於有效地處理字串操作繁重代碼至關重要。

無論您是處理兩個字串還是串接多個字串,C# 提供強大的解決方案確保您的字串串接需求能夠有效滿足。

此外,我們展示了如何將 C# 中的字串串接操作與 IronPDF 結合使用,以將 HTML 字串轉換為 PDF 文檔 With IronPDF。 IronPDF also offers thorough documentation for developers and code examples for PDF creation to guide developers in utilizing its extensive features.

IronPDF 提供免費試用版供下載,供商業用途,並以負擔得起的價格提供許可證。 要了解有關 IronPDF 各種功能的更多信息,請訪問他們的Iron Software 官方網站

常見問題解答

如何在 C# 中串接字串?

在 C# 中,您可以使用 `+` 運算符進行字串串接,它直接將兩個或多個字串連接起來。對於更複雜的情況,`String.Concat` 方法允許您串接多個字串,而 `String.Join` 則允許您使用指定的分隔符串接字串。

C# 中的字串字面量和變數有何區別?

在 C# 中,字串字面量是一個直接嵌入在代碼中的字符序列,以雙引號括起來,例如 "Hello"。另一方面,字串變數是使用 string 關鍵字定義的,並可以容納隨時間變化的字串資料。

如何在 C# 中將串接的 HTML 字串轉換為 PDF?

您可以使用 IronPDF 在 C# 中將串接的 HTML 字串轉換為 PDF。此程式庫允許您使用方法如 RenderHtmlAsPdf 將合併的 HTML 內容渲染為 PDF 文件。

C# 中 String.Concat 方法的用途是什麼?

`String.Concat` 方法在 C# 中用於將多個字串串接為一個單一的字串。它可以接受任意數量的字串參數,因此對於高效地連接多個字串非常有用。

C# 中的 String.Join 方法如何運作?

`String.Join` 方法在 C# 中使用指定的分隔符來串接字串。這對於從陣列中創建句子或清單非常有用,因為它在每個字串之間放置分隔符。

在 .NET 中使用 PDF 程式庫有哪些好處?

在 .NET 中使用像 IronPDF 這樣的 PDF 程式庫允許通過 Chrome 引擎進行準確的 PDF 渲染,支持高需求環境,並提供添加文本、圖像和安全保護 PDF 等功能。

我可以用 C# 中的字串串接自動化生成 PDF 嗎?

是的,您可以使用 IronPDF 在 C# 中透過字串串接自動化生成 PDF。這包括串接 HTML 字串並利用 IronPDF 方法將它們轉換為 PDF 文件。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。