golang

How to Trim a String in Golang

Golang is a popular programming language that offers different functions for dealing with string data. One of the most common operations that we need to perform on strings is trimming or removing extra spaces from the beginning or end of a string. This article covers various ways to trim a string in Golang.

What is String Trimming?

String trimming is a process of removing extra spaces or characters from the beginning or end of a string. The purpose of trimming is to normalize the string, making it consistent and easier to read. Trimming is a common operation in many programming languages, including Golang.

Trim() Function in Golang

The Trim() function is another built-in function in Golang that allows you to remove specific characters from the beginning and end of a string. The Trim() function takes two arguments: a string to trim and a string containing the characters to be removed. Here is an example:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "Hello, World!"
    str = strings.Trim(str, "H!")
    fmt.Println(str)
}

Output

Here, we used the Trim() function to remove the letter H and the exclamation mark “!” from the start and end of the string.

A picture containing background pattern Description automatically generated

TrimSpace() Function in Golang

The TrimSpace() function in Golang can remove all white spaces at the start or end of the string. Once all spaces are removed, a new string will be output containing no extra white space at the start or end. Here is an example:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "   Hello, World!   "
    str = strings.TrimSpace(str)
    fmt.Println(str)
}

Output

The above code will trim all the white spaces at the start and end of the string. The output will be as follows:

A picture containing background pattern Description automatically generated

TrimLeft() and TrimRight() Functions in Golang

The TrimLeft() and TrimRight() functions are similar to the Trim() function, but they remove the specified characters only from the beginning or end of the string. Here is an example:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "!Hello, World!"
    str = strings.TrimLeft(str, "H!")
    fmt.Println(str)
    str = strings.TrimRight(str, "d!")
    fmt.Println(str)
}

Here in the above code, the TrimLeft() function will remove the letter H and the exclamation mark “!” from the beginning of the string and we used the TrimRight() function to remove the letter d and the exclamation mark “!” from the end of the string.

A picture containing background pattern Description automatically generated

TrimPrefix() and TrimSuffix() Functions in Golang

The TrimPrefix() and TrimSuffix() functions are similar to the TrimLeft() and TrimRight() functions, but they remove the specified prefix or suffix from the string. Here is an example:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "Hello, World!"
    str = strings.TrimPrefix(str, "He")
    fmt.Println(str)
    str = strings.TrimSuffix(str, "ld!")
    fmt.Println(str)
}

In the above example, we used the TrimPrefix() function to remove the prefix He from the beginning of the string, and we used the TrimSuffix() function to remove the suffix “ld!” from the end of the string.

A picture containing background pattern Description automatically generated

Conclusion

String trimming in Golang is a process of removing the extra characters or white spaces in the code. In Golang we have different types of trim functions such as TrimSpace(), Trim(), TrimLeft(), TrimRight(), TrimPrefix(), and TrimSuffix(). All these functions are discussed in this article along with examples of how to use them to trim a string in Golang.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.