Lesson 7 of 10

Strings and their methods

Goal

Split, Replace and the StringBuilder class

01

Explanation

A string in C# can't change after it's created, so every method you call on it returns a new string and leaves the original as it was. That's why name.Trim(); on a line by itself computes the result and then throws it away, and the right way is name = name.Trim();. Among the string methods, Trim() removes spaces from both ends, ToUpper() converts letters to upper case, Contains("x") tells you whether the string contains a piece of text, and Replace("a", "b") replaces every occurrence, not just the first. You can chain them like this: code.Trim().ToUpper().

Substring(start, length) cuts out part of the string, and its second parameter is the length of that part, not the position where it ends. It throws an exception if the part runs past the end of the string. Split(',') breaks the string at every comma and returns a string[] array, and string.Join(" | ", parts) does the opposite, joining the items with a separator between them. new string('*', 4) repeats a character four times.

Because every change creates a new string, building a string with += inside a loop copies the whole thing on every pass. A StringBuilder from System.Text collects the pieces in one place instead: Append adds text or a character to the end, and ToString() gives you the complete string when you're done.

02

Your task

Complete NormalizeCode(code) to return the gift code with no spaces at either end and in upper case, and Censor(message, word) to return the message with every occurrence of the word replaced by as many asterisks as the word has letters. Then complete Preview(text, max) to return the first max characters followed by ... if the text is longer than max, and the text unchanged if it isn't, and FormatTags(csv) to take comma-separated tags, trim the spaces around them and join them with " | ". Finally, write HealthBar(hp, maxHp) with a StringBuilder: it starts with [, adds a # for each remaining health point and a - for each lost one until it reaches maxHp, and ends with ], so HealthBar(3, 5) returns [###--].

Checks

6 checks
  1. NormalizeCode trims the spaces and converts the letters to upper case
  2. Censor replaces every occurrence of the word with one asterisk per letter
  3. Preview cuts long text and adds ... at the end
  4. Preview leaves short text unchanged
  5. FormatTags splits the tags, trims the spaces and joins them with " | "
  6. HealthBar draws the bar like [###--]
03

Hints

0/3
Track lessons0 of 10 done

C# is the language behind Unity games and .NET apps. Every variable has a fixed type, programs are built from classes, and it comes with ready-made tools for most of what you need.

Beginner level, 10 lessons, 2.5 hours in total.

Program.cs
OutputC# (Mono 6.6, Judge0)

Compiling and running your code on the server.

–/6

Compiling and running your code on the server.

6 checks listed under “Your task”.

Track lessons0 of 10 done

C# is the language behind Unity games and .NET apps. Every variable has a fixed type, programs are built from classes, and it comes with ready-made tools for most of what you need.

Beginner level, 10 lessons, 2.5 hours in total.