Golang Program to check a string contains a specified substring or not


What is a string and a substring?

A substring in Go is a portion of a larger string. It is specified by providing a start index and a length, and it contains the characters of the original string starting at the start index and going up to the specified length. The characters in a substring are still a part of the original string and share the same memory.

In Go, a string is a sequence of characters. It is an immutable data type, meaning that once a string is created, it cannot be modified. Strings are enclosed in double quotes ("") and can contain any combination of letters, numbers, and symbols.

In this article, we will use for loops and library functions in go language to implement this result. A substring can be considered as the subset of the complete string. The elements of a substring are always present inside the original string.

Method 1: Using A User-Defined Function

In this method, we will create external user-defined functions and pass both the string and substring as arguments to the function. Then based on the value returned by the function we will make the conclusion.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then, we need to start the containsSubstring() function. This function accepts the string as well as substring to be checked as arguments to the function and returns a Boolean value based on whether the substring is found or not.

  • Step 3 − Use a for loop to iterate over the given string. And use a if condition to check whether the current character of substring is present in any of the position of string or not.

  • Step 4 − If the value turns out to be a part of the string then return true otherwise return false.

  • Step 5 − Now, start the main() function. Inside this function initialize a string as well as substring and assign values to them.

  • Step 6 − Call the containsSubstring() function by passing both the string and substring as arguments to the function.

  • Step 7 − if the value returned by the function is true then print that the substring is found in the string otherwise print that the substring is not found in the string.

  • Step 8 − Repeat the process by taking some more examples and print the corresponding result on the screen.

Example

In this example, we will use a for loop to check a string contains a specified substring or not

package main
import "fmt"
func containsSubstring(str, substr string) bool {
   for i := 0; i < len(str)-len(substr)+1; i++ {
      if str[i:i+len(substr)] == substr {
	     return true
	  }
   }
   return false
}
func main() {
   fmt.Println("Example 1:")
   var str string = "Live young live free"
   var substr string = "free"

   if containsSubstring(str, substr) {
      fmt.Printf("The string '%s' contains the substring '%s'.\n", str, substr)
   } else {
      fmt.Printf("The string '%s' does not contain the substring '%s'.\n", str, substr)
   }
   fmt.Println()
   fmt.Println("Example 2:")
   substr = "world"

   if containsSubstring(str, substr) {
      fmt.Printf("The string '%s' contains the substring '%s'.\n", str, substr)
   } else {
      fmt.Printf("The string '%s' does not contain the substring '%s'.\n", str, substr)
   }
}

Output

Example 1:
The string 'Live young live free' contains the substring 'free'.

Example 2:
The string 'Live young live free' does not contain the substring 'world'.

Method 2: Using Internal Function()

In this method, we are going to use two different functions i.e. contains() and index to check whether a string contains a specified substring or not

Syntax

func Contains(str, substr string) bool

The contain() function is present in strings package and is used to check whether a given substring is present in the string or not. The function accepts the two strings as arguments to the function and returns a Boolean value based on whether the substring is found or not. If the value returned by the function is true then the substring is found and vice-versa.

func Index(s, substr string) int

The index() function is present in strings package and is used to get the index of the first occurrence of a given substring. The function takes two values as arguments. One is the string and other is the substring whose occurrence is to be detected. The function then returns the first occurrence of that substring in integer format.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Now, start the main() function. Here initialize a string and substring and assign value to it.

  • Step 3 − Now, check if the value returned by the contains() function is true or not and print the result accordingly.

Example 1

In this example we will use contain() function present in strings package to check a string contains a specified substring or not.

package main
import (
   "fmt"
   "strings"
)

func main() {
   fmt.Println("Example 1:")
   var str string = "Live young live free"
   var substr string = "young"
   if strings.Contains(str, substr) {
      fmt.Printf("The string '%s' contains the substring '%s'.\n", str, substr)
   } else {
      fmt.Printf("The string '%s' does not contain the substring '%s'.\n", str, substr)
   }

   fmt.Println()
   fmt.Println("Example 2:")
   substr = "and"
   if strings.Contains(str, substr) {
      fmt.Printf("The string '%s' contains the substring '%s'.\n", str, substr)
   } else {
      fmt.Printf("The string '%s' does not contain the substring '%s'.\n", str, substr)
   }
}

Output

Example 1:
The string 'Live young live free' contains the substring 'young'.

Example 2:
The string 'Live young live free' does not contain the substring 'and'.

Example 2

In this example we will use index() function present in strings package to check a string contains a specified substring or not.

package main
import (
   "fmt"
   "strings"
)

func main() {
   fmt.Println("Example 1:")
   var str string = "Live young live free"
   var substr string = "live"
   if strings.Index(str, substr) != -1 {
      fmt.Printf("The string '%s' contains the substring '%s'.\n", str, substr)
   } else {
      fmt.Printf("The string '%s' does not contain the substring '%s'.\n", str, substr)
   }
   fmt.Println()
   fmt.Println("Example 2:")
   substr = "either"
   if strings.Index(str, substr) != -1 {
      fmt.Printf("The string '%s' contains the substring '%s'.\n", str, substr)
   } else {
      fmt.Printf("The string '%s' does not contain the substring '%s'.\n", str, substr)
   }
}

Output

Example 1:
The string 'Live young live free' contains the substring 'live'.

Example 2:
The string 'Live young live free' does not contain the substring 'either'.

Conclusion

We have successfully compiled and executed a Golang Program to check a string contains a specified substring or not along with examples. we have implemented three programs in this article in the first program we are simply using for loop to implement the result while in the second and third example we are using contains() and index() inbuilt functions respectively in order to carry the respective operation.

Updated on: 13-Feb-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements