C# String Formatting Challenge: Solution Explained
Practise combining interpolated verbatim strings, Unicode text and escape sequences to display file paths in two languages.
Working solution
This solution uses exactly two console write calls and retains the required variables.
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";
string englishLocation = $@"c:\Exercise\{projectName}\data.txt";
string russianLocation = $@"c:\Exercise\{projectName}\ru-RU\data.txt";
Console.WriteLine($"View English output:\n {englishLocation}\n");
Console.Write($"{russianMessage}:\n {russianLocation}");Expected output
View English output: c:\Exercise\ACME\data.txt Посмотреть русский вывод: c:\Exercise\ACME\ru-RU\data.txt
Why this works
$@"..."keeps path backslashes literal and insertsprojectName.- The backslash before
{projectName}separates the folder names. - The Russian path includes the
ru-RUsubfolder. - Newline escapes and two spaces align the output consistently.
Try the string formatting challenge first, or return to the Module 4 lesson guide.
Reference: Microsoft C# string interpolation documentation.
Check the formatting challenge
Why does the path show {projectName} instead of the value?
Interpolation needs the $ prefix. The @ prefix preserves backslashes but does not substitute variables by itself. With both prefixes, the path can contain literal backslashes and an interpolated project name.
Practice and check
Change ACME to Demo and predict both displayed paths. The English path should contain Demo\data.txt; the Russian path should contain Demo\ru-RU\data.txt. Check line breaks separately from the path characters.
Comments
Post a Comment