Remove all Spaces from a String
To remove all spaces from a string in Go language, we may replace all the spaces with an empty string.
To replace the white spaces with empty string, we can use strings.ReplaceAll() function.
The syntax of strings.ReplaceAll() function to replace spaces with empty string in a given string str
is
</>
Copy
strings.ReplaceAll(str, " ", "")
Example
In the following example, we remove all the spaces in a string, by replacing them with an empty string.
Example.go
</>
Copy
package main
import "strings"
func main() {
str := "Hello World Hello World"
result := strings.ReplaceAll(str, " ", "")
println(result)
}
Output
HelloWorldHelloWorld
Conclusion
In this Golang Tutorial, we learned how to remove spaces from a String in Go language, with example programs.