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.5k Unmarshal/Load CSV record into struct in Go
+17.7k Golang : Read input from console line
+10.5k Golang : Get remaining text such as id or filename after last segment in URL path
+11.8k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+6.3k Golang : Process json data with Jason package
+11.2k Golang : Search and extract certain XML data example
+9.7k Golang : Simple image viewer with Go-GTK
+5.8k Fix sudo yum hang problem with no output or error messages
+13.1k Golang : Recombine chunked files example
+7k Golang : Get final or effective URL with Request.URL example
+19.5k Golang : Clean up null characters from input data