c# · level 1 · unit 3 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Strings come with tricks built in.
A is a trick a value knows how to do. Write the box name, a dot, the method name and brackets. pet.ToUpper() shouts in capitals and pet.ToLower() whispers in small letters.
.Trim() cuts spaces off both ends, handy when someone types an extra space. .Replace("B", "W") swaps every B for a W, like a find-and-swap button.
counts the letters, spaces and symbols. It has no brackets, because it is a fact about the string, not a trick. Each method gives back new text and leaves the box as it was.
using System;
string pet = "Biscuit";
Console.WriteLine(pet.ToUpper());
Console.WriteLine(pet.ToLower());
Console.WriteLine(pet.Replace("B", "W"));
Console.WriteLine(pet.Length); To keep the new text, put it back in the box: pet = pet.ToUpper();.
guess first
Count carefully. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
using System;
string snack = "ice cream";
Console.WriteLine(snack.Length);
your turn
Your turn. Shout a word and measure it.
Type your favourite word between the quotes, then press run to shout it in capitals and count how long it is.
output
press run to see what happens
fix it
This call for help is far too quiet. Can you fix it?
output
press run to see what happens
- The method on line 4 runs, but the box still says help. Where does the new text go?
- Methods give back new text and leave the box as it was. Catch the new text in the box.
- Change line 4 to
shout = shout.ToUpper();, then press run.
nailed it
your strings know some tricks now. :D
- A method is a trick, used with a dot and brackets.
.Lengthhas no brackets. It counts everything in the string.- Methods give back new text. The box stays the same.
new words in your collection
- method
- Length
This lesson isn’t saved. A free account remembers every lesson you finish.