【问题标题】:Powershell to truncate a stringPowershell截断字符串
【发布时间】:2015-12-07 10:54:32
【问题描述】:

我正在尝试截断从 Excel 文件中获取的字符串。

字符串(URL)为:;#://abc.com/sites/abcx/000000103483/default.aspx;#000000103483;#

我需要它是:abc.com/sites/abcx/000000103483/

但问题是 abcx 在从 abcxabcxxxx 的 URL 范围内,其中 x 是一个整数。

我怎样才能得到所需的结果?

【问题讨论】:

    标签: string powershell substring truncation


    【解决方案1】:

    在 PowerShell 中,您可以拆分 /s 并构建您想要的字符串,如下所示:

    $string = ";#://abc.com/sites/abcx/000000103483/default.aspx;#000000103483;#"
    $split = $string.Split("/")
    $split[2] + "/" + $split[3] + "/" + $split[4] + "/" + $split[5] + "/"
    

    输出:

    abc.com/sites/abcx/000000103483/
    

    【讨论】:

      【解决方案2】:

      删除default.aspx 及其后面的所有内容。

      $s = ';#://abc.com/sites/abcx/000000103483/default.aspx;#000000103483;#'
      $s -replace 'default\.aspx.*'
      

      【讨论】:

        猜你喜欢
        • 2015-03-12
        • 2015-12-20
        • 2011-06-23
        • 1970-01-01
        • 2014-09-14
        • 2015-12-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多