Skip to content

Go: How to Check if a String Contains a Substring

Checking if a character or a substring exists in a string is so simple:

// Contains reports whether findstr is within mainstr.
func Contains(mainstr, findstr string) bool

And a simple example:

package main

import (
	"fmt"
	"strings"
)

func main() {
	message := "My name is Morteza an I love Golang!"

	if strings.Contains(message, "Golang") {
		fmt.Println("This user is a Gopher :)")
	} else {
		fmt.Println("Just a simple user :(")
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *