【发布时间】:2017-11-24 12:44:06
【问题描述】:
运行以下
$x = "CF21_flddep-op-config"
$x.TrimEnd("-op-config")
结果:
CF21_fldde
什么时候应该显示:
CF21_flddep
有什么想法吗?
【问题讨论】:
标签: powershell powershell-2.0 powershell-3.0 trim
运行以下
$x = "CF21_flddep-op-config"
$x.TrimEnd("-op-config")
结果:
CF21_fldde
什么时候应该显示:
CF21_flddep
有什么想法吗?
【问题讨论】:
标签: powershell powershell-2.0 powershell-3.0 trim
.TrimEnd() 不会删除尾随字符串,它会删除一组尾随字符。 p 在该集合中,因此最后一个 p 也被删除。 (使用.TrimEnd("-cfginop"),或更明确地.TrimEnd('-', 'c', 'f', 'g', 'i', 'n', 'o', 'p'),您会得到相同的结果。)您想要$x -replace "-op-config", "" 之类的东西,或者,如果字符串只有在它出现在末尾时才能被删除,-replace "-op-config$", ""。
【讨论】:
,"" 部分。由于没有什么可扩展的,我会使用-replace '-op-config$'