×

go · 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

The strings toolbox is full of tricks for text.

counts the characters in a string, spaces included, so len("cat") is 3. For more tricks, add a second line, import "strings", to bring in the strings toolbox.

makes every letter a capital, and strings.ToLower makes them small. snips spaces off both ends. swaps every copy of one piece of text for another.

None of these change your box. They hand back a new string, so store it if you want to keep it: name = strings.TrimSpace(name).

package main
import "fmt"
import "strings"
func main() {
	shout := "  hello there  "
	fmt.Println(len(shout))
	fmt.Println(strings.ToUpper(shout))
	fmt.Println(strings.TrimSpace(shout))
	fmt.Println(strings.ReplaceAll("cats and cats", "cats", "dogs"))
}

Each toolbox gets its own import line at the top, under package main.