英文:
Remove path from filename
问题
我有一个简单的问题。
我有一个包含文件名和路径的字符串。如何删除整个路径?我尝试了以下方法:
line = "/some/path/to/remove/file.name"
line := strings.LastIndex(line, "/")
fmt.Println(line)
它打印出一个奇怪的数字:
38
我需要它没有最后的斜杠。
非常感谢。
英文:
I have trivial question.
I have string which contains a filename and it's path. How can i remove whole path? I have tried those:
line = "/some/path/to/remove/file.name"
line := strings.LastIndex(line, "/")
fmt.Println(line)
It prints some strange number:
38
I need it without last slash
Thanks a lot
答案1
得分: 132
数字是字符串中最后一个斜杠的索引。如果你想获取文件的基本名称,请使用filepath.Base
:
path := "/some/path/to/remove/file.name"
file := filepath.Base(path)
fmt.Println(file)
Playground: http://play.golang.org/p/DzlCV-HC-r。
英文:
The number is the index of the last slash in the string. If you want to get the file's base name, use filepath.Base
:
path := "/some/path/to/remove/file.name"
file := filepath.Base(path)
fmt.Println(file)
Playground: http://play.golang.org/p/DzlCV-HC-r.
答案2
得分: 26
你可以在playground中尝试一下!
dir, file := filepath.Split("/some/path/to/remove/file.name")
fmt.Println("Dir:", dir) //Dir: /some/path/to/remove/
fmt.Println("File:", file) //File: file.name
英文:
<b>You can try it in the playground!</b><br/>
dir, file := filepath.Split("/some/path/to/remove/file.name")
fmt.Println("Dir:", dir) //Dir: /some/path/to/remove/
fmt.Println("File:", file) //File: file.name
答案3
得分: 9
另一个选项:
package main
import "path"
func main() {
line := "/some/path/to/remove/file.name"
line = path.Base(line)
println(line == "file.name")
}
https://golang.org/pkg/path#Base
英文:
Another option:
package main
import "path"
func main() {
line := "/some/path/to/remove/file.name"
line = path.Base(line)
println(line == "file.name")
}
答案4
得分: 2
如果你想要去除文件名的基本路径,你可以使用Dir
函数,它在这里有详细的文档说明:https://golang.org/pkg/path/filepath/#Dir
引用他们文档的一部分:
Dir
函数返回路径除去最后一个元素的部分,通常是路径的目录。在去除最后一个元素后,Dir
函数会对路径进行清理,并移除尾部的斜杠。
此外,在他们的文档中,运行以下代码:
package main
import (
"fmt"
"path/filepath"
)
func main() {
fmt.Println("On Unix:")
fmt.Println(filepath.Dir("/foo/bar/baz.js"))
fmt.Println(filepath.Dir("/foo/bar/baz"))
fmt.Println(filepath.Dir("/foo/bar/baz/"))
fmt.Println(filepath.Dir("/dirty//path///"))
fmt.Println(filepath.Dir("dev.txt"))
fmt.Println(filepath.Dir("../todo.txt"))
fmt.Println(filepath.Dir(".."))
fmt.Println(filepath.Dir("."))
fmt.Println(filepath.Dir("/"))
fmt.Println(filepath.Dir(""))
}
将会得到以下输出:
On Unix:
/foo/bar
/foo/bar
/foo/bar/baz
/dirty/path
.
..
.
.
/
.
你可以在这里自己尝试一下:https://play.golang.org/p/huk3EmORFw5
如果你想要获取不包含基本路径的文件名,@Ainar-G已经给出了足够的答案。
英文:
If you want the base path without the fileName, you can use Dir, which is documented here: https://golang.org/pkg/path/filepath/#Dir
Quoting part of their documentation:
> Dir returns all but the last element of path, typically the path's directory. After dropping the final element, Dir calls Clean on the path and trailing slashes are removed.
Also from their documentation, running this code:
package main
import (
"fmt"
"path/filepath"
)
func main() {
fmt.Println("On Unix:")
fmt.Println(filepath.Dir("/foo/bar/baz.js"))
fmt.Println(filepath.Dir("/foo/bar/baz"))
fmt.Println(filepath.Dir("/foo/bar/baz/"))
fmt.Println(filepath.Dir("/dirty//path///"))
fmt.Println(filepath.Dir("dev.txt"))
fmt.Println(filepath.Dir("../todo.txt"))
fmt.Println(filepath.Dir(".."))
fmt.Println(filepath.Dir("."))
fmt.Println(filepath.Dir("/"))
fmt.Println(filepath.Dir(""))
}
will give you this output:
On Unix:
/foo/bar
/foo/bar
/foo/bar/baz
/dirty/path
.
..
.
.
/
.
Try it yourself here:
https://play.golang.org/p/huk3EmORFw5
If you instead want to get the fileName without the base path, @Ainar-G has sufficiently answered that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论