Golang program to check if a string contains a substring



A substring is a small string in a string and string in Golang is a collection of characters. Since strings in Go are immutable, they cannot be modified after they have been produced. Concatenating or adding to an existing string, however, enables the creation of new strings. A built-in type in Go, the string type can be used in a variety of ways much like any other data type.

Syntax

strings.Contains(str,substring string)

To determine whether a string contains a particular substring, use the Contains(s, substr string) bool function. If the substring is found in the supplied string, a boolean value indicating its presence is returned.

strings.Index(str, substring string)

The int function index(s, str string) is used to determine the index of the first instance of a specified substring within a given string. It returns either -1 if the substring is missing or the index of the substring within the string.

strings.Index(str, substring string)

The int function index(s, str string) is used to determine the index of the first instance of a specified substring within a given string. It returns either -1 if the substring is missing or the index of the substring within the string.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and strings package

  • Step 2 − Create a function main and in that function create a string mystr

  • Step  3 − Using the string function, check whether the string contains the substring or not

  • Step 4 − Print the output

Example 1

In this example we will see how to check if a string contains a substring using a built-in function strings.Contains(). The output will be a Boolean value printed on the console. Let’s see through the code and algorithm to get the concept easily.

package main
import (
	"fmt"
	"strings"
)

func main() {
	mystr := "Hello,alexa!"  //create string
	fmt.Println("The string created here is:", mystr)
	substring := "alexa"   //hold substring
	fmt.Println("The substring from the string is:", substring)
	fmt.Println("Whether the substring is present in string or not?")
	fmt.Println(strings.Contains(mystr, substring))  //use built-in function to check if substring is present in string
}

Output

The string created here is: Hello,alexa!
The substring from the string is: alexa
Whether the substring is present in string or not?
true

Example 2

In this example, we will see how to check if a string contains a substring or not using strings.Index() function.

package main
import (
	"fmt"
	"strings"
)
func main() {
	mystr := "Hello, alexa!" //create string
	fmt.Println("The string created here is:", mystr)
	substring := "alexa"   //substring 
	fmt.Println("The substring from the string is:", substring)
	fmt.Println("Whether the string contains the substring or not?")
	if strings.Index(mystr, substring) >= 0 {    //check if condition is true print the statement
		fmt.Println("The string contains the substring.")
	} else {
		fmt.Println("The string does not contain the substring.")
	}
}

Output

The string created here is: Hello, alexa!
The substring from the string is: alexa
Whether the string contains the substring or not?
The string contains the substring.

Example 3

In this example we will see how to find if a string contains a substring using for loop in strings.Index() function −

package main
import (
	"fmt"
	"strings"
)
func main() {
	mystr := "Hello, alexa!"  //create string
	fmt.Println("The string created here is:", mystr)
	substring := "alexa"  //hold substring
	fmt.Println("The substring present here is:", substring)
	fmt.Println("Whether the substring is present in string or not?")
	found := false
	for i := 0; i < len(mystr); i++ {
		if strings.Index(mystr[i:], substring) == 0 { //check if substring is present in string or not
			found = true
			break
		}
	}
	if found {
		fmt.Println("The string has substring in it.")
	} else {
		fmt.Println("The string does not have substring in it.")
	}
}

Output

The string created here is: Hello, alexa!
The substring present here is: alexa
Whether the substring is present in string or not?
The string has substring in it.

Conclusion

We executed the program of checking if a string contains a substring or not using three examples. In the first example we used strings.Contains() function, in the second example we used strings.Index() function and in the third example we used for loop with the former built-in function.


Advertisements