Strip all white spaces, tabs, newlines from a string
Removing white spaces from strings such as spaces, tabs, and newlines returns a new string without any white space. For example, removing white space in the string "L O N D O N" returns "LONDON".
Remove Space from String
// Golang program to remove
// white-space from string
package main
import (
"fmt"
"unicode"
)
func removeSpace(s string) string {
rr := make([]rune, 0, len(s))
for _, r := range s {
if !unicode.IsSpace(r) {
rr = append(rr, r)
}
}
return string(rr)
}
func main() {
s := "L O N D O N"
fmt.Println(s)
s = removeSpace(s)
fmt.Println(s)
}
Remove Spaces, Tabs, and Newlines from String
// Golang program to remove
// white-space, tabs, newline from string
package main
import (
"fmt"
"strings"
)
func main() {
x := " 1 ,2, 3,\t4,6\n -3 0, 7, 8,70 -9 0"
x = strings.Replace(x, " ", "", -1)
x = strings.Replace(x, "\t", "", -1)
x = strings.Replace(x, "\n", "", -1)
fmt.Println(x)
}
Most Helpful This Week
How to check UPPERCASE characters in a string in Golang?
Golang String Concatenation
How to check if a string contains a white space in Golang?
How to check if a string contains a substring in Golang?
How to check string contains uppercase lowercase character in Golang?
How to remove special characters from a string in GoLang?
How to remove all line breaks from a string in Golang?
How to trim leading and trailing white spaces of a string in Golang?
How to remove multiple spaces in a string in GoLang?
How to check if a string contains only letters in Golang?