如何分割路径名。
输入是 C:\test\deva\tcl\newfiles\aug.txt
输出应该是“test”“deva”“tcl”“newfiles”“
aug.txt”文件或末尾的任何其他“.txt”文件不应打印该字符串。
the input is C:\test\deva\tcl\newfiles\aug.txt
the output should be "test" "deva" "tcl" "newfiles"
"aug.txt" files or anyother ".txt" files at the end of the string should not be printed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恢复到我原来的解决方案并添加一些位...
假设这是一个文件路径而不是一个恰好需要分割的随机字符串 \
文件拆分 几乎可以满足您的要求,它返回拆分为列表的路径。您还想使用 lrange 来选择除卷之外的所有内容,即类似(未经测试)的内容
,因此您没有 c:\ ,它应该是文件分割返回的列表中的第一个元素
此外,如果您有可能获得目录路径而不是文件名,例如重新测试时的相同警告
Reverting back to my original solution and adding some bits...
Assuming this is a filepath not a random string that happens to need to be split \
File split does almost what you want, it returns the path split up as a list . you also want to use lrange to select everything but the volume i.e something like (untested)
so you don't have c:\ which should be the first element in the list returned by file split
Additionally you may want to use file dirname first if there is any chance you will get directory path instead of a filename e.g. same caveats re testing
[split]
与[lrange]
结合可以做你想做的事情,但以不可移植的方式。使其更可移植的一种方法是使用调用
[文件分隔符]
的结果来进行分割,而不是纯粹的“\”。但由于“/”在 Tcl 和 Windows 中也可以使用,因此真正的可移植方法是在字符串上重复调用[file dirname]
并使用提取返回路径名的最后一个组成部分>[文件尾部]
。有关详细信息,请阅读此、这个 和 这个。
[split]
combined with[lrange]
can do what you want but in a non-portable way.One way to make this more portable would be to use the result of calling
[file separator]
for splitting instead of bare "\". But since "/" are also okay both in Tcl and Windows the real way to go portably would be to repeatedly call[file dirname]
on the string and extract the last component of the returned pathname using[file tail]
.For more info read this, this and this.