GoLang String - HasPrefix() and HasSuffix()

In this tutorial, we will learn to identify whether string starts with the given substring and similarly to check string ends with the given substring in Go.

In the Go Language HasPrefix() is the method that check if the string starts with the given substring or not and similarly, HasSuffix() is the method that check if the string ends with the given substring or not. let us explore the uses cases of each separately with help of simple examples.

HasPrefix()

HasPrefix() is the method available in strings package which will return true if the string starts with specified substring. Otherwise, false is returned.

Syntax:

strings.HasPrefix(actual_string,sub_string) It takes two parameters.

Parameters:

actual_string is the string and sub_string is the string to be checked whether the actual_string starts with this substring.

It is important to specify the "strings" package in import.

Example 1:

Let us consider the string - "welcome to gkindex" and check whether it starts with "welcome".

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex"
 
    fmt.Println("String: ", actual_string)

      // Check whether the actual_string starts with "welcome"
     fmt.Println(strings.HasPrefix(actual_string,"welcome"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
true

As per the output, the actual_string starts with "welcome".

Example 2:

Let us consider the string - "welcome to gkindex" and check whether it starts with "gkindex".

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex"
 
    fmt.Println("String: ", actual_string)

      // Check whether the actual_string starts with "gkindex"
     fmt.Println(strings.HasPrefix(actual_string,"gkindex"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
false

As per the output, the actual_string does not start with "gkindex".

Example 3:

Let us consider the string - " welcome to gkindex" and check whether it starts with " ".

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = " welcome to gkindex"
 
    fmt.Println("String: ", actual_string)

      // Check whether the actual_string starts with " "
     fmt.Println(strings.HasPrefix(actual_string," "))
     
}

Output:

CopiedCopy Code

String:   welcome to gkindex
true

As per the output, the actual_string starts with " ".

HasSuffix()

HasSuffix() is the method available in strings package which will return true if the string ends with specified substring. Otherwise, false is returned.

Syntax:

strings.HasSuffix(actual_string,sub_string)

It takes two parameters.

Parameters:

actual_string is the string and sub_string is the string to be checked whether the actual_string ends with this substring.

It is important to specify the "strings" package in import.

Example 1:

Let us consider the string - "welcome to gkindex" and check whether it ends with "gkindex".

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex"
 
    fmt.Println("String: ", actual_string)

      // Check whether the actual_string ends with "gkindex"
     fmt.Println(strings.HasSuffix(actual_string,"gkindex"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
true

As per the output, the actual_string ends with "gkindex".

Example 2:

Let us consider the string - "welcome to gkindex" and check whether it ends with "welcome".

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex"
 
    fmt.Println("String: ", actual_string)

      // Check whether the actual_string ends with "welcome"
     fmt.Println(strings.HasSuffix(actual_string,"welcome"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
false

As per the output, the actual_string does not end with "welcome".

Example 3:

Let us consider the string - "welcome to gkindex " and check whether it ends with " ".

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex "
 
    fmt.Println("String: ", actual_string)

      // Check whether the actual_string ends with " "
     fmt.Println(strings.HasSuffix(actual_string," "))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex 
true

As per the output, the actual_string ends with " ".

Conclusion

Now we know how to check if the string starts with specific substring using HasPrefix() method and string ends with specific substring using HasSuffix() method in the Golang and understood the implementation with working examples.