Basic string formatting in C#

notshanxx

Shanxx

Posted on November 26, 2023

Basic string formatting in C#

Character Escape Sequence

  • An escape character sequence is an instruction to the runtime to insert a special character that will affect the output of your string. In C#, the escape character sequence begins with a backslash \ followed by the character you're escaping.

\ - use case -> Console.WriteLine("Hello \"World\"!"); -> Hello "World"!
-> backslash \ followed by the character you're escaping
\n - new line
\t - new tab big space

Verbatim string literal

  • will keep all whitespace and characters without the need to escape the backslash.
Console.WriteLine(@"    c:\source\repos    
        (this is where your code goes)");
Enter fullscreen mode Exit fullscreen mode
Output
c:\source\repos    
        (this is where your code goes)
Enter fullscreen mode Exit fullscreen mode

Unicode escape characters

  • add encoded characters in literal strings using the \u escape sequence, then a four-character code representing some character in Unicode (UTF-16).
Console.WriteLine("\u3053\u3093\u306B\u3061\u306F World!");
Output -> // Kon'nichiwa World
Enter fullscreen mode Exit fullscreen mode

String Interpolation

int version = 11;
string updateText = "Update to Windows";
Console.WriteLine($"{updateText} {version}!");
Enter fullscreen mode Exit fullscreen mode

Outpt:
Update to Windows 11!

Read Full

💖 💪 🙅 🚩
notshanxx
Shanxx

Posted on November 26, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related