dongshan1959 2016-10-12 10:06
浏览 63
已采纳

字符串用斜杠替换反斜杠

I need to replace \ with / in a path string, but following code failed.

package main

import (
    "fmt"
    "strings"
)

func main() {
    string := "P:\Project\project-name/content/topic/"
    fmt.Println(strings.Replace(string, "\\", "/", -1))
}

Any helpful suggestions?

  • 写回答

3条回答 默认 最新

  • doudang1052 2016-10-12 15:31
    关注

    Use the function filepath.ToSlash to replace the operating system path separator with '/' in a path.

    On Windows, the function returns strings.Replace(path, string(filepath.Separator), "/", -1). On other operating systems, the function returns the path argument as is.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?