How to Trim a String in Golang?


In Golang, trimming a string means removing whitespace characters from the beginning and/or end of the string. Trimming can be useful when working with user input, where leading or trailing whitespace may be unintentionally entered. In this article, we will explore how to trim a string in Golang.

Using the strings.TrimSpace() function

Golang provides a built-in strings package that contains many useful functions for working with strings. The strings.TrimSpace() function can be used to trim leading and trailing whitespace from a string.

Example

Here is an example of using the strings.TrimSpace() function to trim a string −

package main

import (
   "fmt"
   "strings"
)

func main() {
   str := "  Hello, World!  "

   trimmed := strings.TrimSpace(str)

   fmt.Println(trimmed)
}

Output

Hello, World!

In the above example, we have a string named str that contains leading and trailing whitespace. We call the strings.TrimSpace() function with the input string to obtain a new string with the whitespace removed.

Using the strings.Trim() function

The strings package also provides the strings.Trim() function, which can be used to trim specific characters from the beginning and/or end of a string.

Example

Here is an example of using the strings.Trim() function to trim a specific character from a string −

package main

import (
   "fmt"
   "strings"
)

func main() {
   str := "  Hello, World!  "

   trimmed := strings.Trim(str, " ")

   fmt.Println(trimmed)
}

Output

Hello, World!

In the above example, we have a string named str that contains leading and trailing spaces. We call the strings.Trim() function with the input string and the characters to be trimmed as a string. In this case, we pass " " as the second argument to remove spaces from the beginning and end of the string.

Using the strings.TrimLeft() and strings.TrimRight() functions

The strings package also provides the strings.TrimLeft() and strings.TrimRight() functions, which can be used to trim specific characters from the beginning or end of a string, respectively.

Example

Here is an example of using the strings.TrimLeft() and strings.TrimRight() functions to trim specific characters from a string −

package main

import (
   "fmt"
   "strings"
)

func main() {
   str := "  Hello, World!  "

   trimmedLeft := strings.TrimLeft(str, " ")
   trimmedRight := strings.TrimRight(trimmedLeft, "!")

   fmt.Println(trimmedRight)
}

Output

Hello, World!

In the above example, we have a string named str that contains leading and trailing spaces and an exclamation mark at the end. We call the strings.TrimLeft() function with the input string and the characters to be trimmed from the beginning of the string. We then call the strings.TrimRight() function with the result of strings.TrimLeft() and the characters to be trimmed from the end of the string.

Conclusion

Trimming a string in Golang is a simple process using the strings package. The strings.TrimSpace(), strings.Trim(), strings.TrimLeft(), and strings.TrimRight() functions provide flexible and efficient ways to remove whitespace and specific characters from the beginning and/or end of a string.

Updated on: 25-Apr-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements