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
+18.4k Golang : Write file with io.WriteString
+6k PHP : How to check if an array is empty ?
+4.8k Golang : A program that contain another program and executes it during run-time
+10.8k Golang : Removes punctuation or defined delimiter from the user's input
+9.4k Mac OSX : Get a process/daemon status information
+10.6k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example
+3.7k Java : Random alphabets, alpha-numeric or numbers only string generator
+27.5k PHP : Convert(cast) string to bigInt
+7.3k Golang : Individual and total number of words counter example
+14.4k How to automatically restart your crashed Golang server
+7.8k Golang : How to feed or take banana with Gorilla Web Toolkit Session package