Search⌘ K
AI Features

Other Common String Operations

Explore common string operations in Go such as searching for substrings, comparing strings, converting case, and trimming whitespace. Understand the time complexity of these operations and how to apply them to solve text manipulation problems effectively.

We'll cover the following...

Knowing the basic string operations such as finding the length, traversal, indexing, concatenation, slicing, and splitting is an important start. Knowing additional string operations will make you better prepared for a wider range of string algorithms. A problem may ask whether two strings are equal, whether one string starts with another, whether a smaller string appears inside a larger one, or how to form a new string from existing ones.

Searching

Searching refers to the operation of locating a character or a substring within a larger string. At a fundamental level, this is accomplished by scanning through the string and examining each position until the target is found or the end of the string is reached.

Checking for existence

The simplest form of search determines whether a substring exists anywhere within the string. In Go, this is done using strings.Contains.

Go (1.24.2)
package main
import (
"fmt"
"strings"
)
func main() {
s := "hello world"
// Evaluating the presence of the substring "world" within the string s.
// strings.Contains performs a search to determine if the sequence exists.
// A boolean true is returned if the sequence is identified.
fmt.Printf("Is \"world\" present in the string? %t\n\n", strings.Contains(s, "world"))
// Evaluating the presence of the substring "xyz" within the string s.
// A boolean false is returned if the sequence is not identified.
fmt.Printf("Is \"xyz\" present in the string? %t\n", strings.Contains(s, "xyz"))
}

strings.Contains returns true if the substring is found and false otherwise.

Finding the position

When the exact position of the first occurrence is needed, Go provides strings.Index, which returns the starting byte index of the substring. If the substring is not present, strings.Index returns -1.

Go (1.24.2)
package main
import (
"fmt"
"strings"
)
func main() {
s := "hello world"
// Retrieving the starting index of the first occurrence of "world".
// strings.Index returns the lowest byte index where the substring begins.
// If the sequence is identified, an integer index (6) is returned.
fmt.Printf("The starting index of \"world\" in the string: %d\n\n", strings.Index(s, "world"))
// Attempting to retrieve the starting index of the sequence "xyz".
// If the substring is not present within the string, strings.Index returns -1.
fmt.Printf("The starting index of \"xyz\" (not present) in the string: %d\n", strings.Index(s, "xyz"))
}

Go does not provide a separate string search function that raises an exception when the substring is missing. Instead, strings.Index returns -1, and you can check that value to handle the missing-substring case explicitly.

Go (1.24.2)
package main
import (
"fmt"
"strings"
)
func main() {
s := "hello world"
// Retrieving the starting index of the first occurrence of "world".
// strings.Index returns the lowest byte index where the substring begins.
// If the sequence is identified, an integer index (6) is returned.
fmt.Printf("The starting index of \"world\" in the string: %d\n\n", strings.Index(s, "world"))
// Attempting to retrieve the starting index of the sequence "xyz".
// Go returns -1 when the substring is not present, so handle that case explicitly.
index := strings.Index(s, "xyz")
if index == -1 {
fmt.Println("The substring \"xyz\" was not found in the string.")
} else {
fmt.Printf("The starting index of \"xyz\" in the string: %d\n", index)
}
}

When using strings.Index, the important choice is whether -1 should be ...