GoLang String - TrimPrefix() and TrimSuffix()

In this tutorial, we will learn how to remove prefix of a string and similarly remove suffix of a string using in-built methods of GO.

In the Go Language, If you want to remove prefix from the actual string, then TrimPrefix() method is used and to remove the suffix from the string, TrimSuffix() is used. let us explore the uses cases of each separately with help of simple examples.

String TrimPrefix()

TrimPrefix() is the method available in strings package which will remove prefix string from the beginning (left side) of the actual_string. If the prefix string is not present in the actual_string, the actual_string will remain same.

Syntax:


strings.TrimPrefix(actual_string,sub_string)

It takes two parameters. Parameters: actual_string is the string and sub_string is the string which is removed from the beginning of the actual_string if it exists. It is important to specify the "strings" package in import.

Example 1:

Let's consider the string - "welcome to gkindex" and remove "welcome" using TrimPrefix().

CopiedCopy Code

package main

import (
    "fmt"
"strings")

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

      // remove "welcome".
       fmt.Println(strings.TrimPrefix(actual_string,"welcome"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
 to gkindex

Here, we are removing "welcome" from beginning of the actual_string.

Example 2:

Let's consider the string - "welcome to gkindex" and remove "welcome to gk" using TrimPrefix().

CopiedCopy Code

package main

import (
    "fmt"
"strings")

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

      // remove "welcome to gk".
       fmt.Println(strings.TrimPrefix(actual_string,"welcome to gk"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
index

Here, we are removing "welcome to gk" from beginning of the actual_string. so, the final string is - "index".

Example 3:

Let's consider the string - "welcome to gkindex" and remove "gkindex" using TrimPrefix().

CopiedCopy Code

package main

import (
    "fmt"
"strings")

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

      // remove "gkindex".
       fmt.Println(strings.TrimPrefix(actual_string,"gkindex"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
welcome to gkindex

Here, we are removing "gkindex" from beginning of the actual_string. But it is not removed since the "gkindex" is not present at the starting of the string. Hence the actual string remained same.

String TrimSuffix()

TrimSuffix() is the method available in strings package which will remove the suffix string from the last (right side) of the actual_string. If the suffix string is not present in the actual_string, the actual_string will remain same.

Syntax:


strings.TrimSuffix(actual_string,sub_string)

It takes two parameters.

Parameters:

actual_string is the string and sub_string is the string which is removed from the last of the actual_string if it exists. It is important to specify the "strings" package in import.

Example 1:

Let's consider the string - "welcome to gkindex" and remove "gkindex" using TrimSuffix().

CopiedCopy Code

package main

import (
    "fmt"
"strings")

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

      // remove "gkindex".
       fmt.Println(strings.TrimSuffix(actual_string,"gkindex"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
welcome to 

Here, we are removing "gkindex" from the last of the actual_string.

Example 2:

Let's consider the string - "welcome to gkindex" and remove "to gkindex" using TrimSuffix().

CopiedCopy Code

package main

import (
    "fmt"
"strings")

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

      // remove "to gkindex".
       fmt.Println(strings.TrimSuffix(actual_string,"to gkindex"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
welcome 

Here, we are removing "to gkindex" from the last of the actual_string. so, the final string is - "welcome".

Example 3:

Let's consider the string - "welcome to gkindex" and remove "welcome" using TrimSuffix().

CopiedCopy Code

package main

import (
    "fmt"
"strings")

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

      // remove "welcome".
       fmt.Println(strings.TrimSuffix(actual_string,"welcome"))
     
}

Output:

Here, we are removing "welcome" from the last of the actual_string. But it is not removed since the "welcome" is not present at the ending of the string. Hence the actual string remained same.

Conclusion

Now we know how trim prefix and suffix from a string in Golang using TrimPrefix() and TrimSuffix() functions and understood the implementation with working examples.