string 如何在 Golang 中替换字符串中的单个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8190540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:16:56  来源:igfitidea点击:

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

stringreplacechargo

提问by moalf

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.

我正在从用户那里获取物理位置地址,并尝试对其进行安排以创建一个 URL,稍后将使用该 URL 从 Google Geocode API 获取 JSON 响应。

The final URL string result should be similar to this one, without spaces:

最终的 URL 字符串结果应该与这个类似,没有空格:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true

I do not know how to replace white spaces in my URL string and have commas instead. I did read a little about the strings and regexp packages and I have created the following code:

我不知道如何替换我的 URL 字符串中的空格并用逗号代替。我确实阅读了一些关于字符串和正则表达式包的内容,并创建了以下代码:

package main

import (
    "fmt"
    "bufio"
    "os"
    "http"
)

func main() {
    // Get the physical address
    r := bufio.NewReader(os.Stdin)  
    fmt.Println("Enter a physical location address: ")
    line, _, _ := r.ReadLine()

    // Print the inputted address
    address := string(line)
    fmt.Println(address) // Need to see what I'm getting

    // Create the URL and get Google's Geocode API JSON response for that address
    URL := "http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=true"
    fmt.Println(URL)

    result, _ := http.Get(URL)
    fmt.Println(result) // To see what I'm getting at this point
}

回答by Evan Shaw

You can use strings.Replace.

您可以使用strings.Replace.

package main

import (
    "fmt"
    "strings"
)

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

If you need to replace more than one thing, or you'll need to do the same replacement over and over, it might be better to use a strings.Replacer:

如果您需要更换不止一件东西,或者您需要一遍又一遍地进行相同的更换,最好使用strings.Replacer

package main

import (
    "fmt"
    "strings"
)

// replacer replaces spaces with commas and tabs with commas.
// It's a package-level variable so we can easily reuse it, but
// this program doesn't take advantage of that fact.
var replacer = strings.NewReplacer(" ", ",", "\t", ",")

func main() {
    str := "a space- and\ttab-separated string"
    str = replacer.Replace(str)
    fmt.Println(str)
}

And of course if you're replacing for the purpose of encoding, such as URL encoding, then it might be better to use a function specifically for that purpose, such as url.QueryEscape

当然,如果您出于编码目的而进行替换,例如 URL 编码,那么最好使用专门用于该目的的函数,例如 url.QueryEscape

回答by pcampana

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

如果您需要替换字符串中所有出现的字符,请使用strings.ReplaceAll

package main

import (
    "fmt"
    "strings"
)

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