Table of Contents
Different functions to trim leading and trailing spaces in Golang
In this tutorial, we are going to examine some examples of trim leading and trailing white spaces of a string in Golang. Golang provides the built-in package strings which implements simple functions to manipulate UTF-8 encoded strings.
Regarding trimming, In Go, there are numerous functions for trimming strings.
func Trim(s, cutset string) string
func TrimFunc(s string, f func(rune) bool) string
func TrimLeft(s, cutset string) string
func TrimLeftFunc(s string, f func(rune) bool) string
func TrimRight(s, cutset string) string
func TrimRightFunc(s string, f func(rune) bool) string
func TrimSpace(s string) string
Example 1: Using the Trim() function
func Trim(s, cutset string) string
: Trim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.
The code below shows how to trim leading and trailing white spaces using the strings.Trim()
function:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Trim(" Hello GoLinuxCloud members! ", " "))
fmt.Print(strings.Trim("¡¡¡Example 2: Hello GoLinuxCloud members ", "¡"))
}
Output:
Hello GoLinuxCloud members!
Example 2: Hello GoLinuxCloud members
Example 2: Using the TrimSpace() function
func TrimSpace(s string) string
: TrimSpace returns a slice of the string s, with all leading and trailing white space removed, as defined by Unicode.
The example below will trim the leading and trailing white spaces (tab, newline characters included) using the strings.TrimSpace()
function:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.TrimSpace(" \t\n Hello GoLinuxCloud members! \n\t\r\n"))
}
Output:
Hello GoLinuxCloud members!
Example 3: Using the TrimLeft() function to trim the leading spaces
func TrimLeft(s, cutset string) string
: TrimLeft returns a slice of the string s with all leading Unicode code points contained in cutset removed.
Here is an example of how to use the TrimLeft() function in Go:
package main
import (
"fmt"
"strings"
)
func main() {
str := " Hello GoLinuxCloud members! "
fmt.Println("Original length: ", len(str))
fmt.Print(strings.TrimLeft(str, " "))
fmt.Print("After trim the leading length: ", len(strings.TrimLeft(str, " ")))
}
Output:
Original length: 36
Hello GoLinuxCloud members! After trim the leading length: 31
You can see that only the leading spaces are trimmed.
Example 4: Using the TrimRight() function to trim the trailing spaces
func TrimRight(s, cutset string) string
: TrimRight returns a slice of the string s, with all trailing Unicode code points contained in cutset removed.
Here is an example of using the TrimRight()
function in Golang:
package main
import (
"fmt"
"strings"
)
func main() {
str := " Hello GoLinuxCloud members! "
fmt.Println("Original length: ", len(str))
fmt.Print(strings.TrimRight(str, " "))
fmt.Print("After trim the leading length: ", len(strings.TrimRight(str, " ")))
}
Output:
Original length: 36
Hello GoLinuxCloud members!After trim the leading length: 32
Example 5: Using the TrimFunc() function
func TrimFunc(s string, f func(rune) bool) string
: TrimFunc returns a slice of the string s with all leading and trailing Unicode code points c satisfying f(c) removed.
In this example, we will use the TrimFunc()
to remove all the leading and trailing spaces in a string. We will use the unicode.IsSpace()
function to check if a character is a space character or not.
package main
import (
"fmt"
"strings"
"unicode"
)
func main() {
fmt.Print(strings.TrimFunc(" \t Hello GoLinuxCloud members! \n \t ", func(r rune) bool {
return unicode.IsSpace(r)
}))
}
Summary
Go's "strings" package includes the TrimSpace()
and Trim()
functions for trimming leading and trailing spaces from strings.
For more details, please visit the documentation.
References
https://pkg.go.dev/strings#Trim
https://pkg.go.dev/unicode#IsSpace