Home > Back-end >  Add character between words in a string
Add character between words in a string

Time:11-16

Here is some sample code of what I want to achieve

var names string
names = "Adam Eve Noah"

//Unknown code here 

fmt.Println(names) // Output required is "Adam-Eve-Noah"

CodePudding user response:

Use ReplaceAll method from strings library

package main
  
import (
    "fmt"
    "strings"
)

func main() {
    var names string
    names = "Adam Eve Noah"
    names = strings.ReplaceAll(names," ", "-")
    fmt.Println(names)
}
  • Related