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