Search⌘ K
AI Features

Basic String Operations

Explore fundamental string operations in Go, such as slicing, concatenation, splitting, and joining. Understand the behavior and complexity of these operations to write efficient string algorithms and handle immutable strings effectively.

We'll cover the following...

Now that we’ve established that Go strings are immutable and that any apparent modification produces a new string rather than altering the original, the next step is to build familiarity with the operations that are actually available on strings. These operations form the practical foundation of string algorithm implementation. Each one has a defined behavior and a measurable cost, and understanding both is essential before moving on to writing algorithms that use them.

Slicing: Extracting part of a string

Slicing is the operation of extracting a contiguous portion of a string. The extracted portion is called a substring. The original string remains unchanged.

In Go, substring extraction uses the following slice expression:

s[start:end]

The result contains all bytes from the start index up to, but not including, the end index. For simple ASCII strings, this lines up with character positions. Consider the following example:

Go (1.24.2)
package main
import "fmt"
func main() {
// Initialization of the string data
s := "hello world"
// Accessing the first five bytes/characters (indices 0 to 4)
// The character at index 5 (' ') is excluded.
fmt.Printf("Substring from index 0 to 5: \"%s\"\n\n", s[0:5])
// Accessing characters from index 6 to the end (indices 6 to 10)
// This represents the second word in the sequence.
fmt.Printf("Substring from index 6 to 11: \"%s\"\n\n", s[6:11])
// Accessing a shorter prefix (indices 0 to 2)
fmt.Printf("Substring from index 0 to 3: \"%s\"\n", s[0:3])
}

In this example, s[0:5] extracts the first five characters, and s[6:11] extracts the last five. The character at the end index is never included in the result.

Now, let’s look at the following visualizer for a better understanding of slicing in strings:

Omitting start and end in slicing

If the start or end value is omitted, Go uses default bounds:

  • When the start index is omitted, the slice begins at the first character.

  • When the end index is omitted, the slice extends to the last character.

  • When both are omitted, the expression covers the entire string.

Go (1.24.2)
package main
import "fmt"
func main() {
s := "hello world"
// Accessing from the start of the string to index 4 (indices 0 to 4)
// When the start index is omitted, it defaults to 0.
fmt.Printf("Substring from the start to index 5: \"%s\"\n\n", s[:5])
// Accessing from index 6 to the end of the string
// When the end index is omitted, it defaults to the length of the string.
fmt.Printf("Substring from index 6 to the end: \"%s\"\n\n", s[6:])
// Accessing the entire string sequence
// Omitting both indices covers the whole string range.
fmt.Printf("Full string using slicing: \"%s\"\n", s[:])
}

Selecting characters with a step

Go string slicing does not accept a third step argument. When you need every second character, or need to traverse a string backward, you usually write a loop and choose the indices yourself.

sliceWithStep(s, start, end, step)

A loop that increments by two selects every second character, and a loop that decrements from the last index traverses the string in reverse order.

Go (1.24.2)
package main
import (
"fmt"
"strings"
)
func main() {
s := "hello world"
// Go string slicing does not have a step parameter.
// Use a loop to select every second character.
// Indices accessed: 0, 2, 4, 6, 8, 10
var everySecond strings.Builder
for i := 0; i < len(s); i += 2 {
everySecond.WriteByte(s[i])
}
fmt.Printf("Every second character from the string: \"%s\"\n\n", everySecond.String())
// Accessing the string in reverse order
// Move from the end toward the beginning.
var reversed strings.Builder
for i := len(s) - 1; i >= 0; i-- {
reversed.WriteByte(s[i])
}
fmt.Printf("The string in reverse order: \"%s\"\n", reversed.String())
}

Complexity analysis of slicing

Producing an independent substring copy of length kk, for example with ...