DEV Community

Shanxx
Shanxx

Posted on

1

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

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay