Skip to content
Justice Tech School

Strings

Strings are immutable sequences of bytes, usually UTF-8. len gives bytes, so use []rune for characters. The strings package provides ToUpper, Contains, Split, Join, Replace, TrimSpace and more.

Example
package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "Hello, Go"
	fmt.Println(len(s), strings.ToUpper(s), strings.Contains(s, "Go"))
	fmt.Println(strings.Split("a,b,c", ","), strings.Join([]string{"x", "y"}, "-"))
	fmt.Println(strings.Replace(s, "Go", "World", 1), strings.TrimSpace("  hi  "))
	fmt.Println(len("héllo"), len([]rune("héllo")))
}

Result

9 HELLO, GO true
[a b c] x-y
Hello, World hi
6 5

Exercise

Make text upper case.

strings.(s)

Quiz Test

What does len("héllo") count?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.