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
+11.4k Golang : Surveillance with web camera and OpenCV
+13.4k Golang : Convert spaces to tabs and back to spaces example
+6.9k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+12k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+11.8k Golang : Split strings into command line arguments
+26.4k Golang : Convert file content into array of bytes
+11k Golang : How to use if, eq and print properly in html template
+7.3k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+13.8k Golang : Simple word wrap or line breaking example
+17.4k Golang : How to make a file read only and set it to writable again?
+10.7k Golang : Generate random elements without repetition or duplicate