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
+16.8k Golang : Get own process identifier
+9.3k Golang : How to control fmt or log print format?
+19.7k Golang : Set or Add HTTP Request Headers
+8.3k Golang : Configure Apache and NGINX to access your Go service example
+21.7k Golang : Encrypt and decrypt data with TripleDES
+21.3k Golang : How to get time zone and load different time zone?
+6.9k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+9.6k Golang : Extract or copy items from map based on value
+15.3k Golang : Get HTTP protocol version example
+28.7k Golang : Read, Write(Create) and Delete Cookie example
+43.7k Golang : Get hardware information such as disk, memory and CPU usage
+14.2k Golang : syscall.Socket example