Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Remove or Strip all white spaces from a string in Go (Golang)

Posted on February 23, 2023February 23, 2023 by admin

strings.ReplaceAll function can be used to trim all white spaces from a string in Golang. Below is the signature of the function

func ReplaceAll(s, old, new string) string
  • s(first argument) is the input string
  • old(second argument) is the string that has to be replaced with new(third argument) string

Working Code:

package main
import (
"fmt"
"strings"
)
func main() {
sample := " This is a sample string "
noSpaceString := strings.ReplaceAll(sample, " ", "")
fmt.Println(noSpaceString)
}

Output:

Thisisasamplestring
  • all
  • trim
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP