【问题标题】:PowerShell get rid of the last `n`r?PowerShell 摆脱了最后一个 `n`r?
【发布时间】:2014-08-07 17:05:21
【问题描述】:

我正在从文本文件中读取行,并希望去掉 n andr 和结尾空格:

    $g = Get-Content -Encoding ASCII $Tests -ErrorAction Stop | Out-String
    $g.Replace("`r","")
    $g.Replace("`n","")
    $g.trim()

    #Before the a.m. lines I tried unsuccessfully:
    $g.trim("`n`r ")

可能是编码 - 至少没有任何帮助?
我如何摆脱那个“编辑尾巴”

提前致谢
古力

【问题讨论】:

    标签: powershell trim


    【解决方案1】:

    Replace() 不会改变原始字符串,它会返回一个带有替换字符的新字符串Trim() 也一样。你的代码应该是:

    $g = $g.Trim("`n`r ")
    

    如果您不使用Trim(),只是提供一个示例,您的代码与Replace() 应该是:

    $g = $g.Replace("`r", "").Replace("`n", "").Trim()
    

    【讨论】:

    • 谢谢!就是这样,我做了 $g = $(get-content...).trim("nr ") - 完美:)
    猜你喜欢
    • 2019-03-27
    • 2013-06-14
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    相关资源
    最近更新 更多