How to replace a single character inside a string in Golang?

前端 未结 2 581
甜味超标
甜味超标 2020-12-23 16:39

I am getting a physical location address from a user and trying to arrange it to create a URL that would use later to get a JSON response from Google Geocode API.

Th

2条回答
  •  無奈伤痛
    2020-12-23 17:27

    If you need to replace all occurrences of the character in the string, then use strings.ReplaceAll:

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        str := "a space-separated string"
        str = strings.ReplaceAll(str, " ", ",")
        fmt.Println(str)
    }
    

提交回复
热议问题