How to check if a string contains a substring in Go

Go offers quite a few ways to check if a string contains a substring. The canonical way to perform this operation is by using strings.Contains, but we’ll consider some other options as well.

The excellent strings package provides several methods for working with and manipulating strings in Go. The use case of finding out if a substring is present in a string is easily solved using a few methods from this package. Let’s examine each one in turn:

Using strings.Contains

// https://play.golang.org/p/lmGJcyb5RP6
func main() {
	str := "Firetruck"
	fmt.Println(strings.Contains(str, "Fire")) // true
	fmt.Println(strings.Contains(str, "rucks")) // false
}

The strings.Contains method takes a string as the first argument and reports if the second argument string is present in the first. This is the method you will need most of the time.

Using strings.ContainsAny

// https://play.golang.org/p/TUdlCF3-QjN
func main() {
	str := "Firetruck"
	fmt.Println(strings.ContainsAny(str, "abc")) // true
	fmt.Println(strings.ContainsAny(str, "wxyz")) // false
	fmt.Println(strings.ContainsAny(str, "👌")) // false
}

The ContainsAny() method reports if any Unicode character in the second argument is present in the first as shown above.

Using strings.HasPrefix

// https://play.golang.org/p/jehk8QJjDl1
func main() {
	str := "The quick brown fox jumps over the lazy dog"
	fmt.Println(strings.HasPrefix(str, "the")) // false
	fmt.Println(strings.HasPrefix(str, "The")) // true
	fmt.Println(strings.HasPrefix(str, "over")) // false
	fmt.Println(strings.HasPrefix(str, "The quick brown")) // true
}

The HasPrefix method reports if the second argument string is a prefix of the first.

Using strings.HasSuffix

// https://play.golang.org/p/i8FaoOdTczF
func main() {
	str := "The quick brown fox jumps over the lazy dog"
	fmt.Println(strings.HasSuffix(str, "the lazy dog")) // true
	fmt.Println(strings.HasSuffix(str, "fox jumps")) // false
}

Similar to HasPrefix, the HasSuffix method reports if the first string ends with the second.

Using strings.Index

// https://play.golang.org/p/Y2O0Om0zDqJ
func main() {
	str := "The quick brown fox jumps over the lazy dog"
	fmt.Println(strings.Index(str, "jumps")) // 20
	fmt.Println(strings.Index(str, "dog")) // 40
	fmt.Println(strings.Index(str, "silly")) // -1
}

The strings.Index method returns the index of the first instance of the substring (if found) or -1 if the substring is absent.

Using regexp.MatchString

The regexp package can also be used to detect the presence of a substring in Go. I typically do not recommend using this method because it is quite verbose compared to strings.Contains and it’s also slower.

However, it may come in handy if you want to check for the existence of a pattern in a string:

// https://play.golang.org/p/0P15dA6rbBq
func main() {
	str := "The quick brown fox jumps over the lazy dog"
	matched, err := regexp.MatchString("ow.*", str)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(matched) // true
}

For more complex queries, you’ll need to use regex.Compile and the full Regexp interface.

Conclusion

As you can see, there are several ways to determine if a string is present in another string. For most situations, strings.Contains is what you’ll need but it’s also good to know about the other methods in case you have a more specific use case. If you want to extract a substring from a string, that’s covered here as well.

Thanks for reading, and happy coding!