英文:
strings.Trim is removing the letter "i" (golang)
问题
以下是要翻译的内容:
完整路径:views/admin/users.html
修剪集(views):/admin/users.html
修剪集(views/):admin/users.html
完整路径:views/index.html
修剪集(views):/index.html
修剪集(views/):ndex.html
这是我的代码:
err := filepath.Walk("./views", func(path string, info os.FileInfo, err error) error {
if strings.Contains(path, ".html") {
bytes, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
fmt.Println("full path:", path)
fmt.Println("trim set (views):", strings.Trim(path, "views"))
fmt.Println("trim set (views/):", strings.Trim(path, "views/"))
}
}
我是不是疯了?正斜杠与此有关吗?如果你知道,请解释一下发生了什么。
英文:
full path: views/admin/users.html
trim set (views): /admin/users.html
trim set (views/): admin/users.html
full path: views/index.html
trim set (views): /index.html
trim set (views/): ndex.html
Heres my code:
err := filepath.Walk("./views", func(path string, info os.FileInfo, err error) error {
if strings.Contains(path, ".html") {
bytes, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
fmt.Println("full path:", path)
fmt.Println("trim set (views):", strings.Trim(path, "views"))
fmt.Println("trim set (views/):", strings.Trim(path, "views/"))
}
}
Have I lost my mind? Does the forward slash have something to do with this? Please explain what's going on if you know.
答案1
得分: 6
string.Trim()的第二个参数是一个'cutset',即要从字符串中删除的一组符文,其中'i'是其中之一。
要返回除路径的最后一个元素之外的所有元素,请使用path.Dir()。
英文:
The second parameter to strings.Trim() is a 'cutset', ie, a set of runes to remove from the strings, and 'i' is one of them.
To return all but the last element of path, use path.Dir().
答案2
得分: 1
你也可以使用strings.Replace
函数:
fmt.Println("完整路径:", path)
fmt.Println("去除字符串(views):", strings.Replace(path, "views", "", -1))
fmt.Println("去除字符串(views/):", strings.Replace(path, "views/", "", -1))
结果:
完整路径: views/index.html
去除字符串(views): /index.html
去除字符串(views/): index.html
英文:
Also you can use strings.Replace
:
fmt.Println("full path:", path)
fmt.Println("trim set (views):", strings.Replace(path, "views", "", -1))
fmt.Println("trim set (views/):", strings.Replace(path, "views/", "", -1))
Result:
full path: views/index.html
trim set (views): /index.html
trim set (views/): index.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论