Use the function Contains from the strings package.
import (
"strings"
)
strings.Contains("something", "some") // true
Answer from Elliott Beach on Stack OverflowGeeksforGeeks
geeksforgeeks.org › go language › string-contains-function-in-golang-with-examples
strings.Contains Function in Golang with Examples - GeeksforGeeks
April 28, 2025 - Since strings.Contains() function returns boolean value, it returns true in both the cases. Example 2: Following example illustrates how the user can print the desired result instead of a boolean output: ... // Golang program to illustrate // the strings.Contains() Function package main import ( "fmt" "strings" ) func main() { input := "Golang" str := "This is a Golang program" if strings.Contains(str, input) { fmt.Println("Yes") } }
Go Packages
pkg.go.dev › strings
strings package - strings - Go Packages
Fields splits the string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an empty slice if s contains only white space. Every element of the returned slice is non-empty. Unlike Split, leading and trailing runs of white space characters are discarded. ... package main import ( "fmt" "strings" ) func main() { fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) }
Videos
07:44
The strings Package in Go - YouTube
07:04
String Basics in Go - YouTube
18:43
String manipulations - Go programming language tutorial (19) - YouTube
01:47
How to Check If a String Contains an Uppercase Letter in Golang ...
00:37
Golang Tips: Checking if a String Contains a Sub-string #shorts ...
02:00
How to determine if a string contains a regular expression - YouTube
Top answer 1 of 3
432
Use the function Contains from the strings package.
import (
"strings"
)
strings.Contains("something", "some") // true
2 of 3
86
To compare, there are more options:
import (
"fmt"
"regexp"
"strings"
)
const (
str = "something"
substr = "some"
)
// 1. Contains
res := strings.Contains(str, substr)
fmt.Println(res) // true
// 2. Index: check the index of the first instance of substr in str, or -1 if substr is not present
i := strings.Index(str, substr)
fmt.Println(i) // 0
// 3. Split by substr and check len of the slice, or length is 1 if substr is not present
ss := strings.Split(str, substr)
fmt.Println(len(ss)) // 2
// 4. Check number of non-overlapping instances of substr in str
c := strings.Count(str, substr)
fmt.Println(c) // 1
// 5. RegExp
matched, _ := regexp.MatchString(substr, str)
fmt.Println(matched) // true
// 6. Compiled RegExp
re = regexp.MustCompile(substr)
res = re.MatchString(str)
fmt.Println(res) // true
Benchmarks:
Contains internally calls Index, so the speed is almost the same (btw Go 1.11.5 showed a bit bigger difference than on Go 1.14.3).
BenchmarkStringsContains-4 100000000 10.5 ns/op 0 B/op 0 allocs/op
BenchmarkStringsIndex-4 117090943 10.1 ns/op 0 B/op 0 allocs/op
BenchmarkStringsSplit-4 6958126 152 ns/op 32 B/op 1 allocs/op
BenchmarkStringsCount-4 42397729 29.1 ns/op 0 B/op 0 allocs/op
BenchmarkStringsRegExp-4 461696 2467 ns/op 1326 B/op 16 allocs/op
BenchmarkStringsRegExpCompiled-4 7109509 168 ns/op 0 B/op 0 allocs/op
Codecademy
codecademy.com › docs › go › strings › contains()
Go | Strings | Contains() | Codecademy
July 4, 2023 - The Contains() function returns a boolean value indicating whether a given substring is present or not in a given string. The method returns true if the substring is present, otherwise it returns false.
Educative
educative.io › answers › what-is-the-stringcontains-function-in-golang
What is the string.Contains function in Golang?
The Contains function tells us whether a substring appears inside a string or not.
Dot Net Perls
dotnetperls.com › contains-go
Go - strings.Contains and ContainsAny - Dot Net Perls
Is one string contained in another? With Contains we search one string for a certain substring.
Educative
educative.io › answers › how-to-check-if-a-string-contains-a-substring-in-golang
How to check if a string contains a substring in Golang
We can use the Contains() method provided by the strings package to find the string’s substring.
Codekru
codekru.com › home › strings.contains() method in golang
strings.Contains() method in Golang - Codekru
July 31, 2022 - package main import ( "fmt" "strings" ) func main() { str := "codekru" var result1 bool = strings.Contains(str, "code") var result2 bool = strings.Contains(str, "hello") fmt.Println("Is \"code\" present in \"codekru\"? ", result1) fmt.Println("Is \"hello\" present in \"codekru\"?
GitHub
github.com › golang › go › issues › 63328
proposal: strings: function to check if a string contains all specified runes · Issue #63328 · golang/go
October 2, 2023 - Use Case Sometimes I encounter scenarios where I need to check if a given string contains all of a specific set of runes, such as vowels ('a', 'e', 'i', 'o', 'u'). Currently, I have to iterate through the string and runes manually. Propo...
Author hrtsegv
TutorialKart
tutorialkart.com › golang-tutorial › golang-check-if-string-contains-a-substring
How to check if String contains a Substring in Go?
May 28, 2021 - To check if string contains a substring in Go, call Contains function of strings package and provide the string and substring as arguments to the function.
IncludeHelp
includehelp.com › golang › strings-contains-function-with-examples.aspx
Golang strings.Contains() Function with Examples
// Golang program to demonstrate the // example of strings.Contains() Function package main import ( "fmt" "strings" ) func main() { var str1 string = "New Delhi, India" var str2 string = "Los Angeles, California" var substr string substr = "Delhi" if strings.Contains(str1, substr) == true { fmt.Printf("\"%s\" contains \"%s\"\n", str1, substr) } else { fmt.Printf("\"%s\" does not contain \"%s\"\n", str1, substr) } if strings.Contains(str2, substr) == true { fmt.Printf("\"%s\" contains \"%s\"\n", str2, substr) } else { fmt.Printf("\"%s\" does not contain \"%s\"\n", str2, substr) } substr = "Cal
IncludeHelp
includehelp.com › golang › check-a-string-contains-a-specified-substring.aspx
Golang program to check a string contains a specified substring
Golang program to demonstrate the strings.ContainsAny() function
TutorialsPoint
tutorialspoint.com › how-to-use-contains-function-in-golang
How to use Contains() function in Golang?
March 10, 2022 - package main // importing fmt and strings import ( "fmt" "strings" ) // using the strings.Contains() function func main() { fmt.Println(strings.Contains("AndhraPradesh", "Pradesh")) fmt.Println(strings.Contains("MadhyaPradesh", "Madhya")) fmt.Println(strings.Contains("CapitalCity", "Delhi")) fmt.Println(strings.Contains("UttarPradesh", "Uttar")) }
Programming Idioms
programming-idioms.org › idiom › 133 › case-insensitive-string-contains › 1723 › go
Case-insensitive string contains, in Go
function u_i(string, substr) character (len=*), intent(in) :: string, substr integer :: i,j, c1, c2, u_i u_i = 0 out: do i=1,len(string)-len(substr)+1 c1 = iachar(string(i:i)) if (c1 >= iachar('a') .and. c1 <= iachar('z')) c1 = c1 - 32 do j=0,len(substr)-2 c2 = iachar(substr(j+1:j+1)) if (c2 >= iachar('a') .and. c1 <= iachar('z')) c2 = c2 - 32 if (c1 /= c2) cycle out end do u_i = i return end do out end function u_i ok = u_i(string, word) /= 0 · import Data.Char (toLower) import Data.List (isInfixOf) containsIgnoreCase :: String -> String -> Bool containsIgnoreCase s word = isInfixOf (map toLower word) (map toLower s)