0PricingLogin
Go Academy · Lesson

strings Package Functions

Split, join, and trim.

The strings Package

The standard library strings package gives you ready-made helpers for working with text: searching, splitting, joining, trimming, and changing case.

Import it with import "strings".

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.ToUpper("hello"))
}

Contains and HasPrefix

Check for substrings without writing loops:

  • strings.Contains(s, sub) reports if sub appears.
  • strings.HasPrefix and strings.HasSuffix check the start and end.
package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "golang"
    fmt.Println(strings.Contains(s, "lan"))
    fmt.Println(strings.HasPrefix(s, "go"))
}

All lessons in this course

  1. Runes vs Bytes
  2. strings Package Functions
  3. strings.Builder
  4. Unicode and utf8
← Back to Go Academy