Golang Program to check a string starts with a specified substring


In the Go programming language, strings are a built-in data type that represents sequences of characters. They are defined using double quotes (") and can contain any valid Unicode characters. A substring is a portion of a string that contains a sequence of characters from the original string. A substring can be obtained in Go by using the slicing syntax on a string value. In this article we will write a go language program to check whether a string starts with a specified substring or not. Here, we will use both for loops and in built functions in go programming language to achieve the respective result.

Method 1: Using User-defined function

In this method, we are going to use for loop that will iterate over the given string and if the current characters of string match with the substring then we can say that the substring is a part of string.

Algorithm

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

  • Step 2 − Then, start the main() function. Inside the main() initialize two variables of type string and assign values to them. Also initialize a variable called int and store in it the length of substring variable.

  • Step 3 − Further, print both the strings on the screen. Now, start a for loop to iterate over the given string and check if the characters of current string are equal to the substring or not.

  • Step 4 − If the current characters of string match with the substring, then we can say that the substring is a part of string.

  • Step 5 − Then we need to flip the Boolean variable and print that the given string starts with the substring otherwise print that the substring is not a part of string.

Example

In this example we will use a for loop to check whether a string starts with a specified substring or not.

package main
import "fmt"

func main() {
   var s string = "Hello, world!"
   var substring string = "Hello"
   var n int = len(substring)
   var match bool = true
   fmt.Println("The given string is:\n", s)
   fmt.Println()
   fmt.Println("The substring to be checked is:\n", substring)

   for i := 0; i < n; i++ {
      if s[i] != substring[i] {
         match = false
         break
      }
   }

   if match {
      fmt.Println("The given string starts with the specified substring.")
   } else {
      fmt.Println("The given string does not start with the specified substring.")
   }
}

Output

The given string is:
 Hello, world!

The substring to be checked is:
 Hello
The given string starts with the specified substring.

Method 2: Using Internal function

In this method, we are going to use the internal function of Golang – HasPrefix() and Index(). Both of these function are explained below −

Syntax

func HasPrefix(str, prefix string) bool

HasPrefix() function is present in strings package and it is used to check whether a substring is a part of given string or not. The function accepts two arguments one is the string to be checked and other is the substring the function then returns a Boolean variable depending upon whether the string starts with a particular substring or not.

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 and strings package.

  • Step 2 − Then, start the main() function. Inside the main() initialize two strings variables and store in them the string along with the substring that needs to be checked.

  • Step 3 − Further, print the string on the screen. Now, call the HasPrefix() function and pass in it the strings along with the substring that needs to be checked.

  • Step 4 − Store the result returned by the function in a Boolean variable and use if condition to check whether the value returned is true or false.

  • Step 5 − If the value returned is true then print that the string starts with the substring otherwise print that the substring is not a part of string.

Example 1

In this example, we will write a go language program to check whether a string starts with the specified substring or not with the help of HasPrefix() function.

package main
import (
   "fmt"
   "strings"
)

func main() {
   var s string = "Hello, world!"
   var substring string = "world!"
   var match bool
   fmt.Println("The given string is:\n", s)
   fmt.Println()
   fmt.Println("The substring to be checked is:\n", substring)
   match = strings.HasPrefix(s, substring)
   if match {
      fmt.Println("The string starts with the specified substring.")
   } else {
      fmt.Println("The string does not start with the specified substring.")
   }
}

Output

The given string is:
 Hello, world!

The substring to be checked is:
 world!
The string does not start with the specified substring.

Example 2

In this example, we will write a go language program to check whether a string starts with a substring or not with the help of index() function present in strings package.

package main
import (
   "fmt"
   "strings"
)

func main() {
   var s string = "Hello, world!"
   var substring string = "Hello"
   var match bool
   fmt.Println("The given string is:\n", s)
   fmt.Println()
   fmt.Println("The substring to be checked is:\n", substring)
   match = strings.Index(s, substring) == 0
   if match {
      fmt.Println("The string starts with the specified substring.")
   } else {
      fmt.Println("The string does not start with the specified substring.")
   }
}

Output

The given string is:
 Hello, world!

The substring to be checked is:
 Hello
The string starts with the specified substring.

Conclusion

We have successfully compiled and executed a go language program to check whether a string starts with a specified substring or not along with examples. We have used three methods here. In the first method we are using a for loop to implement the logic while in the second and third example we are using inbuilt library functions in go naming HasPrefix() and Index() respectively present in strings package.

Updated on: 17-Feb-2023

420 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements