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.6k Golang : Calculations using complex numbers example
+18.4k Golang : Aligning strings to right, left and center with fill example
+17.2k Golang : When to use init() function?
+24k Golang : Find biggest/largest number in array
+11.1k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+7.5k Golang : Dealing with struct's private part
+9.2k Golang : How to get ECDSA curve and parameters data?
+6.3k Golang : Selection sort example
+6.9k Web : How to see your website from different countries?
+22.1k Golang : Match strings by wildcard patterns with filepath.Match() function
+16.8k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+5.7k Unix/Linux : How to test user agents blocked successfully ?