【发布时间】:2022-01-12 22:49:25
【问题描述】:
基本上我有一条路
"root/main/EVILFOLDER/first/second/third/etc"
我想删除 EVILFOLDER/ 特别是路径的第三段。问题是 EVILFOLDER 可以是任何东西,所以我不能只是硬编码它,路径长度和“/”的数量可以是任何不同的长度。
我有这个,它可以工作,但它不是优雅的。
$path = "root/main/EVILFOLDER/first/second/third/etc"
$basePath = "{0}/{1}/" -f $path.Split('/')
$subFolder = "{3}/" -f $shortPath.Split('/')
$subFolderIndex = $shortPath.IndexOf($subFolder)
$newPath = $basePath + $shortPath.Substring($subFolderIndex)
最终,我希望我可以将它写成类似于“{0}/{1}/{3}...”的内容来获取路径列表的其余部分。想法?
【问题讨论】:
标签: string powershell replace substring