How to find a string in a string in Golang?

Hi 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.

get, find a string in a string, golang

Try it yourself

After understanding the above example you will easily check whether a string contains a substring or not.

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts