从文件名中删除路径 [英] Remove path from filename
本文介绍了从文件名中删除路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有个小问题.
我有一个包含文件名及其路径的字符串.如何删除整个路径?我已经尝试过了:
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
非常感谢
推荐答案
数字是字符串中最后一个斜杠的索引.如果要获取文件的基本名称,请使用 filepath.Base
:
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)
游乐场: http://play.golang.org/p/DzlCV-HC- r .
这篇关于从文件名中删除路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文