Powershell .TrimEnd 没有返回正确的结果

Powershell .TrimEnd not returning correct result

运行以下

$x = "CF21_flddep-op-config"
$x.TrimEnd("-op-config")

结果:

CF21_fldde

应该显示的时间:

CF21_flddep

知道为什么吗?

.TrimEnd() 不会删除尾随 字符串 ,它会删除一组尾随 字符 p 在该集合中,因此最后一个 p 也被删除。 (你会得到与 .TrimEnd("-cfginop") 相同的结果,或者更明确地说 .TrimEnd('-', 'c', 'f', 'g', 'i', 'n', 'o', 'p')。)你想要类似 $x -replace "-op-config", "" 的东西,或者,如果字符串必须只在它出现在最后时被删除,-replace "-op-config$", "".