EN
C# / .NET - escape sequences
0 points
In this article, we would like to show you escape sequences in C#.
Escape sequence | Description |
---|---|
\n | Inserts a new line. |
\t | Inserts a tab. |
\b | Inserts a backspace. |
\r | Inserts a carriage return. |
\f | Inserts a form feed. |
\" | Inserts a double quote character. |
\\ | Inserts a backslash. |
In this example, we use \\
escape sequence to insert backslash into the path
string.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
string path = "C:\\projects\\example";
8
9
Console.WriteLine(path);
10
}
11
}
Output:
xxxxxxxxxx
1
C:\projects\example
In this example, we use \n
escape sequence to insert a new line between the words.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
string text = "line1\nline2\nline3";
8
9
Console.WriteLine(text);
10
}
11
}
Output:
xxxxxxxxxx
1
line1
2
line2
3
line3
In this example, we use \"
escape sequence to insert a double quote character.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
string quote = "\"double-quoted text\"";
8
9
Console.WriteLine(quote);
10
}
11
}
Output:
xxxxxxxxxx
1
"double-quoted text"