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
+10.4k Golang : Bubble sort example
+8.8k Golang : Get curl -I or head data from URL example
+10.6k PHP : Convert(cast) bigInt to string
+15.4k Golang : Get digits from integer before and after given position example
+6.2k Golang : Break string into a slice of characters example
+5.6k Golang : Markov chains to predict probability of next state example
+13.8k Golang : Fix image: unknown format error
+15.7k Golang : How to reverse elements order in map ?
+8.9k Golang : Gonum standard normal random numbers example
+14.9k Golang : Get all local users and print out their home directory, description and group id
+14k Golang : How to pass map to html template and access the map's elements
+10.3k Golang : Resolve domain name to IP4 and IP6 addresses.