【问题标题】:Powershell .TrimEnd not returning correct resultPowershell .TrimEnd 没有返回正确的结果
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 谢谢!替换是更好的选择
    • 当替换为空时,您可以省略 ,"" 部分。由于没有什么可扩展的,我会使用-replace '-op-config$'
    猜你喜欢
    • 2023-03-14
    • 2014-09-13
    • 2012-08-01
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    相关资源
    最近更新 更多