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
+6.2k Golang : Break string into a slice of characters example
+8.6k Golang : Find network service name from given port and protocol
+15k Golang : Find location by IP address and display with Google Map
+19.6k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+9.6k Golang : Get current, epoch time and display by year, month and day
+10.6k Golang : Get UDP client IP address and differentiate clients by port number
+9.9k Golang : Edge detection with Sobel method
+4.8k Golang : Check if a word is countable or not
+5.9k Golang : Extract XML attribute data with attr field tag example
+18.3k Golang : Set, Get and List environment variables
+21.2k Golang : Encrypt and decrypt data with TripleDES
+4.9k Python : Create Whois client or function example