Remove   in string


(Miha) #1

Hello

what is the best option to remove "   " in string? I am getting this in text and I need to remove it.

tnx


(catacombs) #2

You can use the ReplaceAll function from the strings library:

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.ReplaceAll("Hello there, friends", " ", ""))
}

(Ashish Shekar) #3

Ok, it depends on where exactly you want to remove this space character from.

  1. https://golang.org/pkg/strings/#TrimRight - to remove from the right
  2. https://golang.org/pkg/strings/#TrimLeft - to remove from the left
  3. https://golang.org/pkg/strings/#ReplaceAll - to remove from anywhere inside a string

(Yamil Bracho) #4

You can also use string.Trim which remove leading and trailing spaces (&nbsp)