Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何替换最后两个&x27/';Go中字符串中的字符_Go - Fatal编程技术网

如何替换最后两个&x27/';Go中字符串中的字符

如何替换最后两个&x27/';Go中字符串中的字符,go,Go,我尝试了此代码,但它替换了字符串中的所有/ profilePicture := strings.Replace(tempProfile, "/", "%2F", -2) 想要的结果是 tempProfile = "https://firebasestorage.googleapis.com/v0/b/passporte-b9070.appspot.com/o/profilePicturesOfAbmin/original/1492674641download (3).jpg?alt=media

我尝试了此代码,但它替换了字符串中的所有
/

profilePicture := strings.Replace(tempProfile, "/", "%2F", -2)
想要的结果是

tempProfile = "https://firebasestorage.googleapis.com/v0/b/passporte-b9070.appspot.com/o/profilePicturesOfAbmin/original/1492674641download (3).jpg?alt=media"
首先,从以下方面:

Replace返回字符串s的副本,其中前n个非重叠的old实例被new替换。如果old为空,则它在字符串的开头和每个UTF-8序列之后匹配,产生最多k+1个k-rune字符串替换如果n<0,则更换的数量没有限制。(增加强调)

这就解释了为什么你的-2不起作用

解决所述问题的最简单方法可能如下:

tempProfile = "https://firebasestorage.googleapis.com/v0/b/passporte-b9070.appspot.com/o/profilePicturesOfAbmin%2Foriginal%2F1492674641download (3).jpg?alt=media"
但更好的方法可能是对包进行适当的URL编码

parts := strings.Split(tempProfile, "/")
parts = append(parts[:len(parts)-3], strings.Join(parts[len(parts)-3:], "%2F"))
profilePicture := strings.Join(parts, "/")