字符串用斜杠替换反斜杠 [英] string replace backslash with slash
本文介绍了字符串用斜杠替换反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在路径字符串中用/
替换 \
,但是以下代码失败.
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))
}
有什么有用的建议吗?
推荐答案
使用功能 filepath.ToSlash 将路径中的操作系统路径分隔符替换为"/".
Use the function filepath.ToSlash to replace the operating system path separator with '/' in a path.
在Windows上,该函数返回 strings.Replace(path,string(filepath.Separator),"/",-1)
.在其他操作系统上,该函数按原样返回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.
这篇关于字符串用斜杠替换反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文