How to trim/strip white spaces from a String in Golang Examples
- Admin
- Dec 31, 2023
- Golang-examples
You can learn three programs using the Go language’s built-in functions from the ‘Strings’ package in this blog post.
- Delete all whitespaces from a string.
- Delete spaces from the string’s start and end.
- Remove extra spaces, tabs, or newlines from the given string.
How to trim whitespaces from a string in Golang
This program strips any of the white or empty spaces from a string and returns the resulting string.
The standard ‘Strings’ package includes a number of utility string functions. One of the functions is ‘Replace,’ which replaces empty spaces in a given string and returns a new string.
The Replace function in Golang has the following syntax.
func Replace(s, old, new string, n int) string
return a copy of the given string by replacing an empty string.
Here’s an example of using the Strings replace function to remove empty spaces from a string.
package main
import (
"fmt"
"strings"
)
func main() {
str: = " This is a test example "
fmt.Println("Original String: ", str)
fmt.Println("Output String: ", strings.Replace(str, " ", "", -1))
}
When the above example code is compiled and executed, Output is as follows
Original String: This is a test example
Output String: Thisistestexample
How to strip leading and trailing white spaces from a string in Golang?
This program removes the beginning and ending spaces from a string and returns the new string.
The Strings package🔗 provides a wide range of utility string methods. ‘TrimSpace’ is a function that replaces leading and trailing empty spaces in a string and returns the new string.
The TrimSpace function’s syntax is as follows.
func TrimSpace(s string) string
Return a slice of a given string that removes all start and end spaces.
Here’s an example of using the Strings TrimSpace function to remove leading and trailing empty spaces from a string.
package main
import (
"fmt"
"strings"
)
func main() {
str: = " This is a test example "
fmt.Println("Original String: ", str)
fmt.Println("Output String: ", strings.TrimSpace(str))
}
When the above example code is compiled and executed, the Output is as follows
Original String: This is a test example
Output String: This is a test example
How to remove duplicate empty spaces from a string in Golang?
The program removes all spaces from a given string and results in a string with a single space.
This checks for Tab and newline characters in spaces.
In the following program,
- Using the regex package’s MustCompile function to create a regular expression pattern.
- The pattern is
\\s+
,\\s
represents single character matches with a tab or newline, and `+ checks for one or more characters. - Finally call ReplaceAllString, which returns a new string with removed duplicate space characters.
The example below shows how to remove duplicate space substrings from a given string. ReplaceAllString
package main
import (
"fmt"
"regexp"
)
func main() {
str: = " This is a test example "
singleSpacePattern: = regexp.MustCompile(`\s+`)
fmt.Println("Original String: ", str)
fmt.Println("Output String: ", singleSpacePattern.ReplaceAllString(str, " "))
}
Output is
Original String: This is a test example
Output String: This is a test example
Conclusion
In this post, You learned to remove spaces for a given string in multiple ways.