×

java · level 1 · unit 3 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. 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, then the method name and brackets. pet.toUpperCase() shouts in capitals and pet.toLowerCase() 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.

gives the length of a string as an int, counting every letter, space and symbol. Each method gives back new text and leaves the box as it was.

public class Main {
    public static void main(String[] args) {
        String pet = "Biscuit";
        System.out.println(pet.toUpperCase());
        System.out.println(pet.toLowerCase());
        System.out.println(pet.replace("B", "W"));
        System.out.println(pet.length());
    }
}

To keep the new text, store it in the box: pet = pet.toUpperCase();.