How to find a string in a string in Golang?
May 26, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
Today, I’m going to show you how do I find a string in a string in golang, here I’m using the golang Contains()
method to check string contains a sub string or not.
The Contains()
method returns a boolean value based on whether the sub-string contains a string or not, it expects a string in the first parameter and a sub-string as the second parameter.
Let’s start today’s tutorial How to find a string with in a string in Golang?
Well, I’m going to write a program, where I will use the strings.Contains()
method with different parameter examples and then we will see what it is returning.
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Contains("seafood", "foo"))
fmt.Println(strings.Contains("seafood", "bar"))
fmt.Println(strings.Contains("seafood", ""))
fmt.Println(strings.Contains("", ""))
}
When you run the above program you will get the output true
, false
, true
, and true
. let’s check the output.
After understanding the above example you will easily check whether a string contains a substring or not.
All the best 👍