String Manipulations. Introduction to Programming. C# - Lesson Three (3)
Click here for lesson two
Unit 1
Introduction
As a software developer, you’ll need to write C# code to combine and format literal and variable data to create a new value. That value might be displayed, saved to file or sent across the network. Fortunately, C# provides many ways to combine and format data.
Suppose you want to display the output of a command-line application you’re writing. You want to display values including literal text, text in variables, numeric data, and textual data in other languages. How would you format it correctly so that the user can understand what your application is communicating to them?
In this lesson, you’ll use character escape sequences to format literal strings of text to include special characters including tabs and line feeds — even characters from different languages like Kanji or Cyrillic script! You’ll learn how to concatenate two strings together, and will use string interpolation to create a literal string template with replaceable parts.
By the end of this lesson, you’ll be able to control how your data is displayed to end users of your applications.
Learning objectives
In this lesson, you will:
· Create string data containing tabs, new lines, and other special characters
· Create string data containing Unicode characters
· Combine string data into a new string value via concatenation
· Combine string data into a new string value via interpolation
Prerequisites
Beginner level experience with displaying a message to a console using the
Console.WriteLine() and Console.Write() methods. Click here to read up
Beginner level experience with data types, declaring, initializing, setting, and retrieving values from variables. Click here to read up
Unit2
Formatting Literal Strings in C#
C# provides a wealth of options for formatting strings. We’ll only look at a few of the most used in this lesson. However just about anything you can imagine, you could accomplish. The hardest part may be knowing what terminology to use when searching for answers.
Character Escape Sequences
An escape character sequence is a special instruction to the runtime that you want to insert a special character that will affect the output of your string. In C#, the escape character sequences begin with a backslash \ and then include another character. 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 whitespace.
Console.WriteLine("Hello\nWorld!");
Console.WriteLine("Hello\tWorld!");
If you run the code, you’ll see the following output.
Hello
World!
Hello World!
What if you need to insert a double-quotation mark in a literal string? If you don’t use the character escape sequence, you’ll confuse the compiler because it will think you want to terminate the string prematurely … and will not understand the purpose of the characters after the second double-quotation mark.
Console.WriteLine("Hello "World"!")
The .NET Editor will put a red squiggly line under World. But if you attempt to run the code anyway, you would see the following output.
(1,27): error CS1003: Syntax error, ‘,’ expected (1,27): error CS0103: The name ‘World’ does not exist in the current context (1,32): error CS1003: Syntax error, ‘,’ expected
To handle that situation, use the \” escape sequence.
Console.WriteLine("Hello \"World\"!");
If you run the code above, you would see the following output.
Hello “World”!
What if you need to use the backslash for other purposes, like to display a file path?
Console.WriteLine("c:\source\repos");
Unfortunately, C# reserves the backslash for escape sequences, so if you run the code, the compiler will display the following error.
(1,22): error CS1009: Unrecognized escape sequence
The problem is the sequence \s. The \r doesn’t produce an error because it is a valid escape sequence for a carriage return. However, it’s unlikely that you would want to use a carriage return in this context. To solve the problem, you use the \\ to display a single backslash.
Console.WriteLine("c:\\source\\repos");
Escaping the back slash character produces the output you intended.
c:\source\repos
Verbatim String Literal
A verbatim string literal will keep all whitespace and characters without the need to escape the backslash. To create a verbatim string, use the @ directive before the literal string. Two consecutive double-quote characters (“”) are printed as a single double-quote character (“) in the output string.
Console.WriteLine(@" c:\source\repos (""this is where your code goes"")");
Notice that as written in the @ directive, the string spans two lines, the whitespace is kept, and two consecutive double-quote characters are applied as a single double-quote character in the following output:
c:\source\repos (“this is where your code goes”)
Unicode Escape Characters
You can also 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!");
Kon’nichiwa World
Note There are several caveats here. First, some consoles like the Windows Command Prompt will not display all Unicode characters. It will replace those characters with question mark characters instead. Also, the examples used here are UTF-16. Some characters require UTF-32 and therefore require a different escape sequence. This is a complicated subject, and this lesson is only aiming at showing you what is possible. Depending on your need, you may need to spend quite a bit of time learning and working with Unicode characters in your applications.
Exercise — Character Escape Sequences and Verbatim Strings
Suppose you’ve been asked by your manager to create a mockup of a command-line tool that will generate invoices in both English and Japanese. You don’t have to build the actual functionality that generates the invoices yet. You only need to provide the command line’s user interface to internal customers in the billing department for their approval. Your manager asked you to make sure you add formatting to make the current progress of the tool clear. Your manager also asked you to provide instructions for the Japanese users on how to generate invoices in Japanese.
Step 1 — Format the output of the command-line application using character escape sequences
To create the mockup of our command line tool, add the following code in the editor.
Console.WriteLine("Generating invoices for customer \"ABC Corp\" …\n");
Console.WriteLine("Invoice: 1021\t\tComplete!");
Console.WriteLine("Invoice: 1022\t\tComplete!");
Console.WriteLine("\nOutput Directory:\t");
You should see the following output.
Generating invoices for customer “ABC Corp” …
Invoice: 1021 Complete! Invoice: 1022 Complete!
Output Directory:
Step 2 — Format the output of the command-line application using a verbatim literal string
Add the following line of code beneath the code that was added in step 1.
Console.Write(@"c:\invoices");
When you run the code, you should see now the following output that includes the “output directory” instruction.
Generating invoices for customer “ABC Corp” …
Invoice: 1021 Complete! Invoice: 1022 Complete!
Output Directory: c:\invoices
Step 3 — Format the output of the command-line application using unicode escape characters
To complete the mocked up command-line user interface, we’ll add a phrase in Japanese that translates to “To generate Japanese invoices”, then provides a verbatim literal string with the application executable with a flag. We’ll also add some escape sequences for formatting. Add the following code to your application.
Console.Write("\n\n\u65e5\u672c\u306e\u8acb\u6c42\u66f8\u3092\u751f\u6210\u 3059\u308b\u306b\u306f\uff1a\n\t");
// To generate Japanese invoices: // Nihon no seikyū-sho o seisei suru ni wa:
Console.WriteLine(@"c:\invoices\app.exe -j");
The entire code example should look like the following.
Console.WriteLine("Generating invoices for customer \"ABC 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\u 3059\u308b\u306b\u306f\uff1a\n\t");
Console.WriteLine(@"c:\invoices\app.exe -j");
When you run the code, you should see the following output.
Generating invoices for customer “ABC Corp” …
Invoice: 1021 Complete! Invoice: 1022 Complete!
Output Directory: c:\invoices ⽇本の請求書を⽣成するには: c:\invoices\app.exe -j
Recap
Here’s the most important items to remember about formatting literal strings: 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 out correctly depending on the application.
Unit 3
String Concatenation
Often times, you’ll need to combine data from many different sources, including literal strings and variables containing both text and numeric data. In this unit, you’ll learn about two methods to accomplish this by using string concatenation to combine two or more values into a new string.
What is string concatenation?
String concatenation is “programmer speak” for simply combining two or more values into a new value. Unlike addition, the second value is appended to the end of the first value, and so on.
In the following exercise, you’ll write code to concatenate values together.
Step 1: Delete all of the code in the code editor.
Use your mouse to highlight all of the text in the code editor, then select the backspace or del key to remove everything.
Recommended by LinkedIn
Step 2: Concatenate a literal string and a variable.
To concatenate two strings together, you use the string concatenation operator, which is the plus symbol +. Add the following code to the code window:
string firstName = "Charles";
string message = "Hello " + firstName;
Console.WriteLine(message);
Now, run the code. You’ll see the following result in the output console:
Hello Charles
Notice the order — the first string “Hello “ is first in the new string, and the value in the firstName variable is appended to the end of it.
Step 3: Concatenate multiple variables and literal strings.
You can perform several concatenation operations in the same line of code. Modify the code you wrote in Step 2 to the following code:
string firstName = "Charles";
string greeting = "Hello";
string message = greeting + " " + firstName + "!";
Console.WriteLine(message);
Here we create a more complex message by combining several variables and literal strings. Now, run the code. You’ll see the following result in the output console:
Hello Charles!
Step 4: Avoiding intermediate variables.
In steps 2 and 3, we used an extra variable to hold the new string that resulted from the concatenation operation. Unless you have a good reason to do so, you can (and should) avoid using intermediate variables by performing the concatenation operation as you need it.
Modify the code you wrote in Step 3 to the following code:
string firstName = "Charles";
string greeting = "Hello";
Console.WriteLine(greeting + " " + firstName + "!");
Now, run the code. The result in the output console should be the same, however we simplified the code:
Hello Charles!
Recap
The primary ideas you should take away from this exercise:
String concatenation allows you to combine smaller literal and variable strings into a single string. Avoid creating intermediate variables if adding them doesn’t increase readability.
Unit 4
Exercise — String Interpolation
While string concatenation is simple and convenient, string interpolation is growing in popularity in situations where you need to combine many literal strings and variables into a single formatted message.
What is string interpolation?
String interpolation combines multiple values into a single literal string by using a “template” and one or more interpolation expressions. An interpolation expression is a variable surrounded by an opening and closing curly brace symbol { }. The literal string becomes a template when it’s prefixed by the $ character. In other words, instead of writing the following line of code:
string message = greeting + " " + firstName + "!";
You can write this more concise line of code instead:
string message = $"{greeting} {firstName}!";
In this simple example, you save a few keystrokes. You can imagine how much more concise string interpolation can be in more complex operations. Moreover, many find the string interpolation syntax cleaner and easier to read. In the following exercise, we’ll rewrite the previous messages using string interpolation
Step 1: Delete all of the code in the code editor
Use your mouse to highlight all of the text in the code editor, then select the backspace or del key to remove everything.
Step 2: Use string interpolation to combine a literal string and a variable value
To interpolate two strings together, you create a literal string and prefix the string with the $ symbol. The literal string should contain at least one set of curly braces {} and inside of those characters you use the name of a variable. Add the following code to the code window:
string firstName = "Charles";
string message = $"Hello {firstName}!";
Console.WriteLine(message);
Now, run the code. You’ll see the following result in the output console:
Hello Charles!
Step 3: Use string interpolation with multiple variables and literal strings
You can perform several interpolation operations in the same line of code. Modify the code you wrote in Step 2 to the following code:
string firstName = "Charles";
string greeting = "Hello";
string message = $"{greeting} {firstName}!";
Console.WriteLine(message);
Now, run the code. You’ll see the following result in the output console:
Hello Charles!
Step 4: Avoid intermediate variables
Just as we did in the previous exercise, we can eliminate the temporary variable to store the message. Modify the code you wrote in Step 3 to the following code:
string firstName = "Charles";
string greeting = "Hello";
Console.WriteLine($"{greeting} {firstName}!");
Now, run the code. The result in the output console should be the same, however we simplified the code:
Hello Charles!
Step 5: Combine verbatim literals and string interpolation
Suppose you need to use a verbatim literal in your template. You can use both the verbatim literal prefix symbol @ and the string interpolation $ symbol together.
Delete the code from the previous steps, and type the following code into the .NET Editor.
string projectName = "First-Project";
Console.WriteLine($@"C: \Output\{projectName}\Data");
Now, run the code and you should see the following result.
C:\Output\First-Project\Data
Recap
The primary ideas you should take away from this exercise:
String interpolation provides an improvement over string concatenation by reducing the number of characters required in some situations. You can combine string interpolation and verbatim literals by combining the symbols for each and using that as a prefix for the string template.
Unit 5
Challenge
Step 1: Delete all of the code in the .NET Editor from the earlier exercise.
Select all of the code in the .NET Editor then select the del or backspace key to delete it.
Step 2: Start with two mandatory lines of code.
Begin solving the challenge with the following two lines of code.
string projectName = "ACME";
string russianMessage = "\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0440\u0443\u0441\u0441\u043a\u0438\u0439 \u0432\u044b\u0432\u043e\u0434";
The projectName variable will be used twice in the desired output. The russianMessage variable contains the message “View Russian output” in Russian. You must use this variable in your code that prints the message. You may not change these two lines of code, but you can add code above and below each line. You must use these two lines of code to form the desired output.
Code challenges throughout these modules will reinforce what you’ve learned and help you gain some confidence before continuing on. In this challenge, you’ll print instructions to the end user to let them know where your application will output data files. We won’t be actually creating any files — we’re only interested in displaying formatted instructions to the console window. You’ll use what you’ve learned about character escape sequences, verbatim strings, unicode, and string interpolation to provide instructions in both English and Russian.
Step 3: You may only use either the Console.WriteLine() or the Console.Write() - method twice.
In other words, to complete this challenge, you can only create two instructions that actually print output to the console. If you need to print additional new lines or add any formatting, you must use what you’ve learned in this lesson to accomplish it.
Step 4: Use character escape sequences, verbatim strings, unicode, and string interpolation to generate the output. To complete this challenge, your code must produce the following output.
View English output: c:\Exercise\ACME\data.txt
Посмотреть русский вывод: c: \Exercise\ACME\ru-RU\data.txt
Take note of the new lines, the tabs, and how the two mandatory lines of code are used in the output.
Solution
The following code is one possible solution for the challenge from the previous unit.
string projectName = "ACME";
string englishLocation = $@"c:\Exercise\{projectName}\data .txt";
Console.WriteLine($"View English output:\n\t\t{englishLocation}\n");
string russianMessage = "\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0440\u0443\u0441\u0441\u043a\u0438\u0439 \u0432\u044b\u0432\u043e\u0434";
string russianLocation = $@"c:\Exercise\{projectName}\ru-RU\data.txt";
Console.WriteLine($"{russianMessage}:\n\t\t{russianLocation}\n");
This code is merely “one possible solution”. You may have some variation in naming variables or in the character escape sequences you used. You may have used
Console.Write()
instead of
Console.WriteLine().
You may have attempted to combine everything together without using several variables. However, as long as your code follows the instructions from the challenge and produces the desired output, then congratulations!
Important
If you had trouble completing this challenge, maybe you should review the previous units before you continue on. All new ideas we discuss in other modules will depend on your understanding of the ideas that were presented in this module.
Thank you for reading see you in Lesson Four
Previous Lessons: