Menu Close

Go – how to find substring in string in go golang ?

Here, we are going to learn about finding substring in string in go golang. We can find substring in string by using Contains() function of strings package in go golang.

Function prototype:

func Contains(str string, substr string) bool

Return value:

It returns bool value. If substring is present in string then it returns “true” else “false“.

Example:

package main

import (
	"fmt"
	"strings"
)

func main() {

	// 'foo' presents in 'animalsfood'
	fmt.Println(strings.Contains("animalsfood", "foo"))
	
	// 'bar' does not present in 'birdsfood'
	fmt.Println(strings.Contains("birdsfood", "bar"))
	
	fmt.Println(strings.Contains("animalsfood", ""))
	
	fmt.Println(strings.Contains("", ""))
}

Output:

true
false
true
true

To learn more about golang, Please refer given below link.

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/strings/
Posted in golang, packages, strings

Leave a Reply

Your email address will not be published.

Contact Us