How to trim a string in Golang?

We can trim a string in several ways, and many inbuilt functions in the Go language can help us do the same. This post will discuss the trimming-related methods of the strings package in detail.

Let’s have a look at all of the methods one by one.

Trim() Method

  • Method declaration – func Trim(s, cutset string) string
  • What does it do? It will remove all of the leading and trailing Unicode code points contained in the cutset string from the string s
  • What does it return? It will return a slice of the string after removing all leading and trailing Unicode code points contained in the cutset string, and if either of the strings is empty, it will return the original string ( s ) unchanged.

The Trim() method is used to trim a string of characters in the cutset string.

Code Example
package main

import (
	"fmt"
	"strings"
)

func main() {

	string1 := strings.Trim("**codekru**", "*")        // this will return "codekru"
	string2 := strings.Trim("*#codekru*#", "*#")       // this will return "codekru"
	string3 := strings.Trim("###*codekru*###", "*#")   // this will return "codekru"
	string4 := strings.Trim("###*codekru*#!!##", "*#") // this will return "codekru*#!!"

	fmt.Println("Strings after using the Trim() function:")

	fmt.Println(string1)
	fmt.Println(string2)
	fmt.Println(string3)
	fmt.Println(string4)

}

Output –

Strings after using the Trim() function:
codekru
codekru
codekru
codekru*#!!

Here if we focus on string3, then we know that the order of the cutset string does not matter, and the Unicode code points present at the leading or trailing will still be removed if any matches from the cutset string.

TrimSpace() Method

  • Method declaration – func TrimSpace(s string) string
  • What does it do? It will remove all of the leading and trailing whitespaces from the string passed in the argument
  • What does it return? It will return a slice of the string after removing all leading and trailing whitespaces from it.

TrimSpace() is similar to the Trim() function if we use a space as the cutset string. So, we can say that TrimSpace(s) will produce the same results as Trim(s," ").

Code Example
package main

import (
	"fmt"
	"strings"
)

func main() {

	string1 := "   hello codekru   "
	string2 := "  Hi"

	fmt.Println("Before Trimming the space:")
	fmt.Println(string1, string2)

	string1 = strings.TrimSpace(string1)
	string2 = strings.TrimSpace(string2)

	fmt.Println("After Trimming the space:")
	fmt.Println(string1, string2)

}

Output –

Before Trimming the space:
   hello codekru      Hi
After Trimming the space:
hello codekru Hi

TrimLeft() Method

What if we want to remove the leading Unicode code points only and not the trailing ones? This is where the TrimLeft() function can help you.

  • Method declaration – func TrimLeft(s, cutset string) string
  • What does it do? It will remove only the leading Unicode code points contained in the cutset string ( passed as the second argument ) from the string s ( passed as the first argument )
  • What does it return? It will return a slice of the string after removing all of the leading Unicode code points contained in the cutset string, and if either of the strings passed in the arguments is empty, it will return the original string ( s ) unchanged.
Code Example
package main

import (
	"fmt"
	"strings"
)

func main() {

	string1 := strings.TrimLeft("**codekru**", "*")        // this will return "codekru**"
	string2 := strings.TrimLeft("*#codekru*#", "*#")       // this will return "codekru*#"
	string3 := strings.TrimLeft("###*codekru*###", "*#")   // this will return "codekru*###"
	string4 := strings.TrimLeft("###*codekru*#!!##", "*#") // this will return "codekru*#!!##"

	fmt.Println("Strings after using the TrimLeft() function:")

	fmt.Println(string1)
	fmt.Println(string2)
	fmt.Println(string3)
	fmt.Println(string4)

}

Output –

Strings after using the TrimLeft() function:
codekru**
codekru*#
codekru*###
codekru*#!!##

We can see here that TrimLeft() only trimmed the leading code points, not the trailing ones.

TrimRight() Method

As TrimLeft() only removed the leading Unicode code points, the TrimRight() function will remove only the trailing Unicode code points.

  • Method declaration – func TrimRight(s, cutset string) string
  • What does it do? – It will only remove the trailing Unicode code points contained in the cutset string from the string s.
  • What does it return? – It will return a slice of the string s after removing all of the trailing Unicode code points contained in the cutset string. It will return the same string back if either of the strings passed in the function’s arguments is empty.
Code Example
package main

import (
	"fmt"
	"strings"
)

func main() {

	string1 := strings.TrimRight("**codekru**", "*")        // this will return "**codekru"
	string2 := strings.TrimRight("*#codekru*#", "*#")       // this will return "*#codekru"
	string3 := strings.TrimRight("###*codekru*###", "*#")   // this will return "###*codekru"
	string4 := strings.TrimRight("###*codekru*#!!##", "*#") // this will return "###*codekru*#!!"

	fmt.Println("Strings after using the TrimRight() function:")

	fmt.Println(string1)
	fmt.Println(string2)
	fmt.Println(string3)
	fmt.Println(string4)

}

Output –

Strings after using the TrimRight() function:
**codekru
*#codekru
###*codekru
###*codekru*#!!

Now, let’s move to our last two methods – TrimPrefix() and TrimSuffix()

TrimPrefix() and TrimSuffix() methods are a bit from all of the above methods as the majority of the above methods treat the cutset string as a bunch of Unicode code points bundled together, which they can remove from the string s without concerning about the order of those Unicode code points in the cutset string.

But in TrimPrefix() and TrimSuffix(), the string passed as the second argument is to be treated as a whole and not as individual Unicode code points.

TrimPrefix() Method

  • Method declaration – func TrimPrefix(s, prefix string) string
  • What does it do? It will remove the leading prefix string from the string s if present. If the prefix string is present in the string s but not at the leading position, it will not remove the prefix string from the string s.
  • What does it return? It will return the string s after removing the prefix string from it, and if the prefix string is not present at the starting of the string s, then it will return the original string unchanged.
Code Example
package main

import (
	"fmt"
	"strings"
)

func main() {

	string1 := "**Hi codekru**"
	string2 := "**codekru **Hi"

	string1 = strings.TrimPrefix(string1, "**Hi")
	string2 = strings.TrimPrefix(string2, "**Hi")

	fmt.Println("Strings after using the TrimPrefix() function:")

	fmt.Println(string1)
	fmt.Println(string2)
}

Output –

Strings after using the TrimPrefix() function:
 codekru**
**codekru Hi**

Here we can see that the TrimPrefix() was able to remove the prefix string from string1 because it was present at the leading position, but for string2, it was not able to do so as the string was present at the trailing position.

The internal implementation of the TrimPrefix() method
  • TrimPrefix() method internally uses the HasPrefix() method to first check whether the prefix string ( passed as the second argument in the TrimPrefix() method ) is present as the prefix of string s or not.
  • If it’s present as the prefix, then TrimPrefix() method will just return the string starting from the length of the prefix‘s string till the end of the string s by doing s[len(prefix):].
  • And if the string is not present as the prefix, then TrimPrefix() will return the original string as it is.
func TrimPrefix(s, prefix string) string {
	if HasPrefix(s, prefix) {
		return s[len(prefix):]
	}
	return s
}

TrimSuffix() Method

  • Method declaration – func TrimSuffix(s, suffix string) string
  • What does it do? – It will help remove the suffix string ( passed as the second argument ) if it’s present at the end of the specified string s. So, if the suffix string is present within the string s but it’s not at the end of the string, then the TrimSuffix() method will not remove it from the string s and will return the original string unchanged
  • What does it return? – It will return the string s after removing the suffix string from it, and if the suffix string is not present at the end of the string s, then it will return the original string unchanged
Code Example
package main

import (
	"fmt"
	"strings"
)

func main() {

	string1 := "**Hi codekru**"
	string2 := "**codekru **Hi"

	string1 = strings.TrimSuffix(string1, "**Hi")
	string2 = strings.TrimSuffix(string2, "**Hi")

	fmt.Println("Strings after using the TrimSuffix() function:")

	fmt.Println(string1)
	fmt.Println(string2)
}

Output –

Strings after using the TrimSuffix() function:
**Hi codekru**
**codekru

Here the suffix string is removed from string2 only as it was present at the end of the string s.

The internal implementation of the TrimSuffix() method
  • TrimSuffix() method internally uses the HasSuffix() method to check whether the suffix string is present as the suffix or not.
  • If the string is present as the suffix, then TrimSuffix() simply returns the string s from starting point upto the len(s) – len(suffix) which is done by using s[:len(s)-len(suffix)]
  • And if the string is not present, it will return the original string unchanged.
func TrimSuffix(s, suffix string) string {
	if HasSuffix(s, suffix) {
		return s[:len(s)-len(suffix)]
	}
	return s
}

Please visit this link to learn more about the Strings in Go language and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.