How to check if a string contains a substring in Go? From beginner to pro
\n\nHave you ever been writing Go and suddenly froze in front of a simple question: "Does this string contain the part I need?" If so, you are not alone. On Stack Overflow, this question has garnered thousands of views, and for good reason. Go has several elegant ways to solve this problem, but which one to choose? Let's figure it out once and for all.
\n\nIn this article, we will not just show you the strings.Contains function. We will teach you to think like a Go developer: choose the optimal tool for each situation, avoid pitfalls, and write code that is fast and predictable.
Table of Contents
\n- \n
- strings.Contains — your first friend \n
- strings.Index — when you need more than yes/no \n
- ContainsAny and ContainsRune — fine-tuning \n
- Performance: what to choose? \n
- Edge cases and pitfalls \n
- Summary: what to remember \n
1. strings.Contains — your first friend
\n\nLet's start with the simplest and most popular method. The strings.Contains function from the Go standard library is what you need in 80% of cases. It returns true if the string contains the specified substring, and false otherwise.
Here's what it looks like in real code:
\n\npackage main\n\nimport (\n "fmt"\n "strings"\n)\n\nfunc main() {\n str := "Hello, Go world!"\n substr := "world"\n\n if strings.Contains(str, substr) {\n fmt.Println("The string contains the substring:", substr)\n } else {\n fmt.Println("Substring not found")\n }\n}\n\n\nOutput:
\nThe string contains the substring: world\n\nSimple, right? The function is case-sensitive: strings.Contains("Go", "go") will return false. If you need to ignore case, you'll have to use strings.ToLower or strings.EqualFold.
Example with case-insensitive search
\n\nstr := "Golang is awesome"\nsubstr := "golang"\n\nif strings.Contains(strings.ToLower(str), strings.ToLower(substr)) {\n fmt.Println("Found (case-insensitive)!")\n}\n\n\nWhen to use: when you need a simple binary answer — "yes" or "no". Without unnecessary details.
\n\n2. strings.Index — when you need to know the position
\n\nSometimes it's not enough to know that a substring exists. You want to understand where exactly it is located. For example, you are parsing a log and want to extract part of a string after a certain marker. This is where strings.Index comes to the rescue.
This function returns the index of the first occurrence of the substring (starting from 0) or -1 if the substring is not found.
\n\nstr := "Error: file not found in /var/log"\nsubstr := "file"\n\npos := strings.Index(str, substr)\nif pos != -1 {\n fmt.Printf("Substring found at position %d\\n", pos)\n fmt.Printf("Context: %s\\n", str[pos:])\n} else {\n fmt.Println("Substring not found")\n}\n\n\nOutput:
\nSubstring found at position 7\nContext: file not found in /var/log\n\nThere are other variations:
\n- \n
strings.LastIndex— searches for the last occurrence \n strings.IndexAny— searches for the first occurrence of any character from a set \n strings.IndexRune— searches for a specific Unicode character \n
// Example with LastIndex\nstr := "a-b-c-d"\nlastDash := strings.LastIndex(str, "-")\nfmt.Println("Last dash at position:", lastDash) // 5\n\n// Example with IndexAny\nstr2 := "hello world"\npos := strings.IndexAny(str2, "aeiou")\nfmt.Println("First vowel at po