Saturday, December 22, 2018

Golang Examples - remove/strp white spaces from a String

In this blog post, You will learn three programs using Strings package builtin 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 the all white or empty spaces from a given string, return the new string Standard inbuilt Strings package provides various utility string functions. Replace is one of the function replace empty spaces from a given string and returns a new string.
Following is a syntax for Replace function
func Replace(s, old, new string, n int) string
return copy of the given string with replaces with an empty string.
Here is an Example for removing empty spaces from a string using 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 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, return the new string Standard inbuilt Strings package provides various utility string functions. TrimSpace is one of the function replace leading and trailing empty spaces from a given string and returns 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 remove.
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, Output is as follows
Original String:    This is test example
Output String:  This is 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, return a string with single space. This checks spaces for Tab, newline characters. In the below program, Created a regular expression pattern using regex package with MustCompile function.
The pattern is \s+, \s represents single character matches with a tab or new line, + checks for one or more characters finally call ReplaceAllString, which returns a new string with removed duplicate space characters Following is an example for remove 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 test             example
Output String:   This is test example

Related article


EmoticonEmoticon