← Back to Go Course | Chapter 14: Standard Library & HTTP | Lesson 1 of 10

The strings Package

The strings package is a toolbox full of little helpers for slicing, searching, and reshaping text.
Syntax
go
strings.Contains(s, substr)
strings.Split(s, sep)
strings.Join(parts, sep)
strings.Replace(s, old, new, n)

Searching Within Strings

strings.Contains, HasPrefix, and HasSuffix check whether a string contains, starts with, or ends with a given substring, returning a simple boolean without needing manual index math.

Example: Searching Within Strings

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strings"
)

// main is where the program starts running
func main() {
	// Short declaration: create s and infer the type
	s := "Go is fun"
	// Print the values followed by a newline
	fmt.Println(strings.Contains(s, "fun"))
	// Print the values followed by a newline
	fmt.Println(strings.HasPrefix(s, "Go"))
	fmt.Println(strings.HasSuffix(s, "fun"))
}

Splitting and Joining

strings.Split breaks a string into a slice of substrings around a separator, while strings.Join does the opposite, combining a slice of strings into one, joined by a given separator.

Example: Splitting and Joining

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strings"
)

// main is where the program starts running
func main() {
	// Short declaration: create parts and infer the type
	parts := strings.Split("apple,banana,cherry", ",")
	// Print the values followed by a newline
	fmt.Println(parts)
	// Short declaration: create joined and infer the type
	joined := strings.Join(parts, " | ")
	// Print the values followed by a newline
	fmt.Println(joined)
}

Case Conversion and Trimming

strings.ToUpper and ToLower normalize case, while strings.TrimSpace removes leading and trailing whitespace -- all common steps before comparing or displaying user-provided text.

Example: Case Conversion and Trimming

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strings"
)

// main is where the program starts running
func main() {
	// Short declaration: create raw and infer the type
	raw := "  Hello Go  "
	// Print the values followed by a newline
	fmt.Println(strings.ToUpper(strings.TrimSpace(raw)))
}

Replacing Substrings

strings.ReplaceAll swaps every occurrence of a substring for another, while strings.Replace lets you limit how many occurrences are replaced with an explicit count argument.

Example: Replacing Substrings

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strings"
)

// main is where the program starts running
func main() {
	// Short declaration: create s and infer the type
	s := "one two two three"
	// Print the values followed by a newline
	fmt.Println(strings.ReplaceAll(s, "two", "2"))
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting strings.Split on a separator not found in the string still returns a one-element slice containing the whole original string.
  2. Using strings.Replace with the wrong count argument (n), not realizing 0 replaces nothing and -1 replaces all occurrences.
  3. Assuming strings.Contains is case-insensitive -- it's a strict substring match; use strings.EqualFold or lowercase both sides for case-insensitive comparisons.
Chapter Summary
  • strings.Contains, strings.HasPrefix, and strings.HasSuffix check for substrings at various positions.
  • strings.Split breaks a string into a slice using a separator; strings.Join does the reverse.
  • strings.ToUpper/ToLower/TrimSpace normalize text for comparison or display.
  • strings.Replace and strings.ReplaceAll substitute occurrences of a substring.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.