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
+9.2k Golang : Fix type interface{} has no field or no methods and type assertions example
+3.1k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+3.3k Golang : Tell color name with OpenCV example
+14.7k Golang : Save map/struct to JSON or XML file
+3.4k Golang : Embedded or data bundling example
+7.9k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+13.4k Golang : How to run Golang application such as web server in the background or as daemon?
+2.3k Golang : How to use if, eq and print properly in html template
+1.5k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
844 Golang : Example of how to detect which type of script a word belongs to
+6.6k Golang : Chunk split or divide a string into smaller chunk example