{

How to remove/strip white spaces from a String in Golang Examples


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

Example program to strip all whitespaces from a string

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 copy of the given string with replacing an empty string.

Here is an Example for removing empty spaces from a string using the Strings replace function.

package main  
  
import (  
 "fmt"  
 "strings"  
)  
  
func main() {  
 str := "  This is 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  

Example program to strip leading and trailing spaces from a string

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 for removing leading and trailing empty spaces from a string using Strings TrimSpace function

package main  
  
import (  
 "fmt"  
 "strings"  
)  
  
func main() {  
 str := "  This is 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  

Example program 3 to remove duplicate empty spaces from a string

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, `+ 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 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.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.