Introduction
Escape characters play a pivotal role in C# while formatting messages/strings with special characters for better readability and functionality. We will explore techniques including "\e" introduced in C#13 along with some existing tricky cases like backslashes and quotes and the benefits of verbatim strings for managing complex text such as file paths.
Agenda
In this article, we will explore/understand the following topics.
- "\e" Escape Char in C#13
- Raw String Literals
- Common Escape char(s) Scenarios
"\e" Escape Char in C#13
In .NET 9, handling special characters within strings has been enhanced with features like raw string literals and the introduction of the \e escape sequence in C# 13.
So, let's understand more about writing sample code.
Before .NET9, We usually use Hexa decimal code (\x1b) or Unicode (\u001b).
The below code: [35m part helps to print in pink color text, and [34m helps to print in blue color code.
In .NET 9 - "\e" is an easy and readable way to use for escape char scenarios.
public static void EscapeCharInNET9()
{
//BEFORE C#13
var welcomeMsgHex = "\x1b[35mWelcome to new C# corner site (csharp.com)\x1b[0m";
var welcomeMsgUnicode = "\u001b[34mWelcome to our new C# corner site (csharp.com)\u001b[0m";
Console.WriteLine(welcomeMsgHex);
Console.WriteLine(welcomeMsgUnicode);
//C#13
var welcomeMsgNewFormat = "\e[34mWelcome to new C# corner site (csharp.com) in C#13 way!!\e[0m";
Console.WriteLine(welcomeMsgNewFormat);
}
The output of the aforesaid code is as follows.
![C# output]()
Raw String Literals
C# also supports raw string literals; It allows our development to define multi-line strings without the need for escape sequences. This feature is particularly useful for embedding code snippets, JSON, or XML directly into our C# code.
The sample code is as follows.
public static void RawStringLiteralsDemo()
{
var sampleJson = """
{
"name": "ABC Ltd",
"establish-year": 2022,
"city": "Hyderabad"
}
""";
Console.WriteLine(sampleJson);
}
The output screen for the code is as follows.
![Output screen]()
Common Escape char(s) Scenarios
In our usual development activity, while dealing with strings, we may need to include special formatting like new lines, tabs, or double quotes. This is where escape characters become useful. These are sequences that start with a backslash (\), followed by a character that represents a specific function.
Here are some commonly used escape characters in C#:
- \n – Moves text to a new line.
- \t – Inserts a horizontal tab.
- \" – Allows double quotes within a string.
- \\ – Adds a backslash to the text.
Using these escape sequences makes string manipulation more flexible and readable in C#. The following example code illustrates their usage in a very easy and understandable way.
public static void CommonEscapeCharsDemo()
{
var lastName = "Repalle";
var firstName = "Ramchand";
// Sample greet message using escape characters
string greetMessage = $"Hello! \n{firstName} \t {lastName} !!";
Console.WriteLine(greetMessage);
// File location example with verbatim @ string
string fileLocation = @"C:\Ramchand\Test\sample.txt";
Console.WriteLine(fileLocation);
// Sample message with double quotes and other characters
var sampleMessage = @"Hello all, The new ""csharp.com"" site has the best UI ever.";
Console.WriteLine(sampleMessage);
// File path example with double backslash (\\)
string fileLocation2 = "C:\\Ramchand\\Test\\sample.txt";
Console.WriteLine(fileLocation2);
}
The aforesaid code output is as follows.
![Output]()
The source code (EscapeChars.zip) is also attached to this article for reference.
Conclusion
In this article, we explored escape characters introduced in C#13("\e"), Raw string literals, and other common day-to-day usage of escape char scenarios which helps format strings/messages in a better readable and understandable way.