In this blog post, You will learn three programs using the Strings
package’s built-in functions.
- Delete all whitespaces from a string
- Delete leading and trailing spaces of a string
- Remove duplicate spaces/tabs/newlines from a given string
How to trim whitespaces from a string in Golang
This program is to remove all white or empty spaces from a given string and returns the new string.
The standard inbuilt Strings
package provides various utility string functions. Replace
is one of the functions, to replace empty spaces from a given string and return a new string.
Following is a syntax for Replace function in Golang
func Replace(s, old, new string, n int) string
return a copy of the given string by replacing an empty string.
Here is an example of removing empty spaces from a string using the Strings replace function.
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 spaces from a string in Golang?
This program is to remove the Start and end spaces from a given string and returns the new string.
Standard inbuilt Strings package provides various utility string functions.
TrimSpace
is one of the functions that replace leading and trailing empty spaces from a given string and returns the new string.
Following is a syntax for Replace function
func TrimSpace(s string) string
return a slice of a given string with all start and ending spaces removed.
Here is an Example of removing leading and trailing empty spaces from a string using the Strings TrimSpace function
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?
This program removes duplicate spaces( more than one space) from a given string and returns a string with a single space.
This checks spaces for Tab, and newline characters.
In the below program,
Created a regular expression pattern using the regex package with the MustCompile function.
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.
Following is an example of removing 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.