Post

C# - Working with Strings

In C#, you can use the string data type to store text. For example:

1
string myString = "Hello, World!";

Character escape sequences

  • 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.
  • For example, the \n sequence will add a new line, and a \t sequence will add a tab.

The following code uses escape character sequences to add newlines and tabs:

1
2
3
4
5
6
7
8
string myString
    = "Hello, World!\n\tWelcome to C# Programming!";

Console.WriteLine(myString);

// Output:
// Hello, World!
//     Welcome to C# Programming!

Verbatim string literals

  • A verbatim string literal is a string that ignores escape character sequences
  • You create a verbatim string literal by prefixing the string with the @ symbol.
  • For example, the following code uses a verbatim string literal to print a file path:
1
string filePath = @"C:\Users\Alice\Documents\MyFile.txt";
  • Verbatim string literals are useful when you need to include backslashes or spaces in your string. For example, when working with file paths or regular expressions.
1
2
Console.WriteLine(@"    c:\source\repos
        (this is where your code goes)");

Unicode escape sequences

  • Unicode escape sequences are used to represent characters that are not on the keyboard.
  • You create a Unicode escape sequence by using the \u escape character followed by the Unicode code point in hexadecimal format.
  • For example, the following code uses a Unicode escape sequence to print the Greek letter Ω:
1
2
3
4
5
string myString = "\u03A9";
Console.WriteLine(myString);

// Output:
// Ω
1
2
3
4
5
6
7
8
9
10
11
Console.WriteLine("Generating invoices for customer \"Contoso Corp\" ...\n");
Console.WriteLine("Invoice: 1021\t\tComplete!");
Console.WriteLine("Invoice: 1022\t\tComplete!");
Console.WriteLine("\nOutput Directory:\t");
Console.Write(@"c:\invoices");

// To generate Japanese invoices:
// Nihon no seikyū-sho o seisei suru ni wa:
Console.Write("\n\n\u65e5\u672c\u306e\u8acb\u6c42\u66f8\u3092\u751f\u6210\u3059\u308b\u306b\u306f\uff1a\n\t");
// User command to run an application
Console.WriteLine(@"c:\invoices\app.exe -j");
  • The above code will output the following text:
1
2
3
4
5
6
7
8
9
10
Generating invoices for customer "Contoso Corp" ...

Invoice: 1021            Complete!
Invoice: 1022            Complete!

Output Directory:
c:\invoices

日本の請求書を生成するには:
    c:\invoices\app.exe -j

Formatting String Literals

  • Use character escape sequences when you need to insert a special character into a literal string, like a tab \t, new line \n, or a double quotation mark \".
  • Use an escape character for the backslash \\ when you need to use a backslash in all other scenarios.
  • Use the @ directive to create a verbatim string literal that keeps all whitespace formatting and backslash characters in a string.
  • Use the \u plus a four-character code to represent Unicode characters (UTF-16) in a string.
  • Unicode characters may not print correctly depending on the application.

Concatenating Strings in C#

In C#, you can concatenate strings using the + operator. For example:

1
2
3
4
5
string firstName = "Alice";
string lastName = "Smith";
string fullName = firstName + " " + lastName;

Console.WriteLine(fullName); // Output: Alice Smith

String Interpolation

String interpolation is a way to construct strings that include variables. You can use string interpolation by prefixing a string with the $ symbol and using curly braces {} to include variables. For example:

1
2
string myName = "Alice";
string myGreeting = $"Hello, {myName}!";

Combining Interpolation and Verbatim Strings

  • You can combine string interpolation with verbatim strings to create more readable code.
  • For example, the following code uses string interpolation to create a message with a variable and a verbatim string literal:
1
2
string projectName = "The-Project";
Console.WriteLine($@"/home/user/Documents/Coding/{projectName}/");
This post is licensed under CC BY 4.0 by the author.