Golang : How to check if a string contains another sub-string?
Problem :
How to check if a string contains another sub-string in Golang?
Solution :
Use the strings.Contains()
function.
For example :
package main
import (
"fmt"
"strings"
)
func main() {
str := "this is a string containing a big string and small string"
subStr := "big string"
if strings.Contains(str, subStr) {
fmt.Printf("Found subStr in str \n")
} else {
fmt.Printf("subStr is not in str \n")
}
subStr2 := "another string"
if strings.Contains(str, subStr2) {
fmt.Printf("Found subStr in str \n")
} else {
fmt.Printf("subStr2 is not in str \n")
}
}
Output :
Found subStr in str
subStr2 is not in str
See also : Golang : Smarter Error Handling with strings.Contains()
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+17.2k Golang : Write file with io.WriteString
+23.3k Golang : Change file read or write permission example
+5.1k Golang : Detect face in uploaded photo like GPlus
+8.9k Golang : Print how to use flag for your application example
+19.5k Golang : Create and resolve(read) symbolic links
+5.3k Grep : How to grep for strings inside binary data
+18.2k Golang : How to Set or Add Header http.ResponseWriter?
+17.2k Golang : Example for RSA package functions
+7.5k Golang : Convert(cast) []byte to io.Reader type
+7.3k Golang : Implementing class(object-oriented programming style)
+4.9k Golang : How to verify input is rune?
+32.6k Golang : How to stream file to client(browser) or write to http.ResponseWriter?