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条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试