.NET 帮助 C# 多行字符串(开发者如何使用) Curtis Chau 已更新:七月 28, 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 When you're getting started in C#, strings are one of the earliest concepts you learn. If you’ve ever put together a Hello, World! program, then you’ve already worked with strings. As you get more familiar with C#, your programs will get more complex. Before long, you’ll start working with strings that span multiple lines. If you’re at that stage now, then you’re in luck, because in this guide, we’ll be explaining what multiline strings are and how to use them. Let’s dive in. What is a Multiline String? Multiline strings are exactly what they sound like - a string that spans multiple lines, contrasting to the usual single-line string. A multiline string in C# is created using string literals, which are a series of characters enclosed in double quotes. To form multiline strings, we need to use a special type of string literals called verbatim string literals. The Magic of Verbatim Strings To create a multiline string literal in C#, we use verbatim strings. Verbatim strings are prefixed with the @ symbol and allow us to include line breaks, special characters, and even whitespace without using escape sequences. Here's an example of a multiline string using a verbatim string: string str = @"This is a multiline string in C#."; string str = @"This is a multiline string in C#."; Dim str As String = "This is a multiline string in C#." $vbLabelText $csharpLabel The string str contains three lines, and each line is separated by a line break. Notice the use of the @ symbol to create the verbatim string. Escaping Characters in Verbatim Strings There may be cases where you need to include double quotes within a verbatim string. To do this, you have to use double-double quotes. 例如: string str = @"This is a ""multiline"" string in C#."; string str = @"This is a ""multiline"" string in C#."; Dim str As String = "This is a ""multiline"" string in C#." $vbLabelText $csharpLabel In this example, the string str contains the word "multiline" enclosed in double quotes, which is achieved by using double-double quotes within the verbatim string. Concatenating Multiline Strings You might often need to combine multiline strings with other strings or values. To concatenate a multiline string with other string values, you can use the + operator, just like you would with single-line strings. string name = "John"; string str = @"Hello, " + name + @", Welcome to the world of C# multiline strings!"; string name = "John"; string str = @"Hello, " + name + @", Welcome to the world of C# multiline strings!"; Dim name As String = "John" Dim str As String = "Hello, " & name & ", Welcome to the world of C# multiline strings!" $vbLabelText $csharpLabel Here, we’ve concatenated the multiline string with a single line string name. Formatting Multiline Strings When working with multiline strings, you can format them with variables or expressions. To format a multiline string, you can use the $ symbol for string interpolation. int age = 25; string str = $@"Hello, I am {age} years old, and I am learning C# multiline strings!"; int age = 25; string str = $@"Hello, I am {age} years old, and I am learning C# multiline strings!"; Dim age As Integer = 25 Dim str As String = $"Hello, I am {age} years old, and I am learning C# multiline strings!" $vbLabelText $csharpLabel We used string interpolation to include the value of the age variable within the multiline string. Converting Multiline Strings to Single Line Strings Sometimes, you may need to convert multiline strings into single line strings. To achieve this, you can use the Replace method to replace line breaks with a space or any other desired character. string multilineString = @"This is a multiline string in C#."; string singleLineString = multilineString.Replace(Environment.NewLine, " "); string multilineString = @"This is a multiline string in C#."; string singleLineString = multilineString.Replace(Environment.NewLine, " "); Dim multilineString As String = "This is a multiline string in C#." Dim singleLineString As String = multilineString.Replace(Environment.NewLine, " ") $vbLabelText $csharpLabel We replaced the line breaks in the multiline string with a space, resulting in a single line string. Handling Long Strings Sometimes, very long strings can are difficult to read in your code. To make the code more readable, you can split long strings into multiple lines using the + operator. string longString = "This is a very long string that " + "needs to be split across " + "multiple lines for better readability."; string longString = "This is a very long string that " + "needs to be split across " + "multiple lines for better readability."; Dim longString As String = "This is a very long string that " & "needs to be split across " & "multiple lines for better readability." $vbLabelText $csharpLabel We split the long single-line string into three parts and concatenated them using the + operator, making the code more readable. Creating Multiline String Literals with Raw String Literals In C# 10, a new feature was introduced called raw string literals. These allow us to create multiline string literals without using the @ symbol. To create a raw string literal, we enclose the string in triple quotes ("""). Raw string literals preserve all the characters, including line breaks, within the triple quotes. 例如: string str = """This is a multiline string using raw string literals in C# 10."""; string str = """This is a multiline string using raw string literals in C# 10."""; Dim str As String = """This is a multiline string using raw string literals in C# 10.""" $vbLabelText $csharpLabel We created a multiline string using raw string literals. Notice the use of triple quotes to enclose the string. Escaping Characters in Raw String Literals Similar to verbatim strings, we might need to include double quotes within our raw string literals. To do this, we can use double-double quotes. 例如: string str = """This is a ""multiline"" string using raw string literals in C# 10."""; string str = """This is a ""multiline"" string using raw string literals in C# 10."""; Dim str As String = """This is a ""multiline"" string using raw string literals in C# 10.""" $vbLabelText $csharpLabel We included double quotes within the raw string literal by using double-double quotes. Combining Verbatim and Raw String Literals In some cases, we may want to combine the features of both verbatim and raw string literals. This can be done by using the @ symbol followed by triple quotes. string str = @"""This is a multiline string using both verbatim and raw string literals in C# 10."""; string str = @"""This is a multiline string using both verbatim and raw string literals in C# 10."""; Dim str As String = """This is a multiline string using both verbatim and raw string literals in C# 10.""" $vbLabelText $csharpLabel In this example, we created a multiline string using both verbatim and raw string literals. Notice the use of the @ symbol followed by triple quotes. Using Multiline Strings to Generate PDFs in IronPDF IronPDF is a lightweight .NET PDF library designed specifically with web developers in mind. It makes reading, writing, and manipulating PDF files a breeze, able to convert all kinds of file types into PDF content, and you can use it in your .NET projects for both desktop and web. The best part - it’s free to try out in a development environment. Here’s how to utilize an HTML multiline string to create a PDF file in IronPDF: string name = "John"; string age = "25"; string content = $@"<!DOCTYPE html> <html> <body> <h1>Hello, {name}!</h1> <p>You are {age} years old.</p> <p>This is a multiline string used to generate a PDF with IronPDF and dynamic content.</p> </body> </html>"; // Create a new PDF document using IronPDF var pdfDocument = new IronPdf.ChromePdfRenderer(); pdfDocument.RenderHtmlAsPdf(content).SaveAs("c://BioData.pdf"); string name = "John"; string age = "25"; string content = $@"<!DOCTYPE html> <html> <body> <h1>Hello, {name}!</h1> <p>You are {age} years old.</p> <p>This is a multiline string used to generate a PDF with IronPDF and dynamic content.</p> </body> </html>"; // Create a new PDF document using IronPDF var pdfDocument = new IronPdf.ChromePdfRenderer(); pdfDocument.RenderHtmlAsPdf(content).SaveAs("c://BioData.pdf"); Dim name As String = "John" Dim age As String = "25" Dim content As String = $"<!DOCTYPE html> <html> <body> <h1>Hello, {name}!</h1> <p>You are {age} years old.</p> <p>This is a multiline string used to generate a PDF with IronPDF and dynamic content.</p> </body> </html>" ' Create a new PDF document using IronPDF Dim pdfDocument = New IronPdf.ChromePdfRenderer() pdfDocument.RenderHtmlAsPdf(content).SaveAs("c://BioData.pdf") $vbLabelText $csharpLabel In this example, we used string interpolation to include the name and age variables within our multiline string, making our PDF content dynamic and customizable. We created a multiline string with the content we wanted in the PDF. We then instantiated the ChromePdfRenderer class from the IronPDF namespace and used the RenderHtmlAsPdf method to convert the multiline string content into a PDF document. Finally, we saved the PDF document to a file called "BioData.pdf". 结论 And that’s a whistlestop tour of the versatile world of multiline strings. Now you’re a multiline string C# pro, you can start implementing them in your projects - such as the example with IronPDF above. Looking to get your hands on IronPDF? 您可以从我们的IronPDF的30天免费试用开始。 它也完全免费用于开发目的,因此您可以真正了解它的功能。 And if you like what you see, IronPDF starts as low as $799 for an IronPDF license. For even bigger savings, check out the Iron Suite package where you can get all nine Iron Software tools for the price of two. 祝您编码愉快! 常见问题解答 如何在 C# 中创建多行字符串? 在 C# 中,可以通过在字符串前加上 '@' 符号来使用逐字字符串字面值创建多行字符串。这允许直接在字符串中包含换行符和特殊字符。 在 C# 中使用逐字字符串字面值的目的是什么? C# 中的逐字字符串字面值用于创建保留换行符和特殊字符而不需要转义序列的字符串。它们由 '@' 符号指示,是编写多行字符串的理想选择。 如何在逐字字符串中处理像双引号这样的特殊字符? 在逐字字符串中,可以使用双双引号来包含双引号。例如:'@"This is a ""multiline"" string."' 允许在字符串中包含双引号。 C# 10 中的原始字符串字面值是什么,它们如何工作? C# 10 中引入的原始字符串字面值允许无需使用 '@' 符号来创建多行字符串。它们用三重引号括起来,并保留所有字符,包括换行符,完全如同它们的外观。 多行字符串如何用于 PDF 生成? 多行字符串可用于创建 HTML 格式的动态内容,然后可以使用诸如 IronPDF 等支持 HTML 转 PDF 的库将其转换为 PDF 文档。 什么是字符串插值,它如何应用于多行字符串? C# 中的字符串插值允许使用 '$' 符号在字符串中嵌入变量或表达式。它可以与多行字符串一起使用,以无缝地包含动态内容。 如何在 C# 中将多行字符串转换为单行字符串? 要将多行字符串转换为单行字符串,可以使用Replace方法将换行符替换为空格或其他字符。例如:multilineString.Replace(Environment.NewLine, " ")。 为什么开发人员应该在 .NET 应用程序中使用 PDF 生成库? 像 IronPDF 这样的专为 .NET 环境设计的 PDF 生成库可以简化读取、写入和操纵 PDF 的过程。它们支持将各种文件类型转换为 PDF,提供了一个强大的工具集,适用于桌面和 Web 应用程序。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已更新九月 4, 2025 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多 已更新九月 4, 2025 C# String Equals(开发者用法) 与强大的 PDF 库 IronPDF 结合使用,切换模式匹配允许您为文档处理构建更智能、更简洁的逻辑。 阅读更多 已更新八月 5, 2025 C# Switch 模式匹配(开发者用法) 与强大的 PDF 库 IronPDF 结合使用,切换模式匹配允许您为文档处理构建更智能、更简洁的逻辑。 阅读更多 Visual C++ Redistributable 是什么C# If(开发者如何使用)
已更新九月 4, 2025 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多