Skip to content

Strings

Beginner · Fundamentals

A Go string is an immutable sequence of bytes, usually UTF-8. The strings package has the utilities; unicode/utf8 and []rune handle characters.

  • strings1strings package helpers (ToLower, ReplaceAll)
  • strings2 — bytes vs runes (len vs utf8.RuneCountInString)

The strings package holds the everyday string utilities.

Show hint
Chain two strings helpers:
return strings.ReplaceAll(strings.ToLower(s), " ", "-")

Source

A Go string is bytes, not characters. len(s) counts BYTES; one multibyte

Show hint
len counts bytes; count runes instead:
return utf8.RuneCountInString(s)

Source