.NET HELP

C# Multiline String (How it Works for Developers)

Published August 15, 2024
Share:

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#."
VB   C#

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. For example:

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#."
VB   C#

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!"
VB   C#

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!"
VB   C#

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, " ")
VB   C#

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.";
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

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. For example:

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."""
VB   C#

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. For example:

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."""
VB   C#

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."""
VB   C#

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>";
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>";
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

// Create a new PDF document var pdfDocument = new IronPdf.ChromePdfRenderer(); pdfDocument.RenderHtmlAsPdf(content).SaveAs("c://BioData.pdf");

And here’s what that code looks like in action:

Csharp Multiline String 1 related to Using Multiline Strings to Generate PDFs in IronPDF

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".

Conclusion

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? You can start with our 30-day free trial of IronPDF. It’s also completely free to use for development purposes so you can really get to see what it’s made of. And if you like what you see, IronPDF starts as low as $749 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. Happy coding!

Csharp Multiline String 2 related to Conclusion

< PREVIOUS
What is Visual C++ Redistributable
NEXT >
C# If (How it Works for Developers)

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 11,781,565 View Licenses >