stringpowershellsubstringtruncation

Powershell to truncate a string


I'm trying to truncate a string which I'm fetching from an excel file.

The string(URL) is: ;#://abc.com/sites/abcx/000000103483/default.aspx;#000000103483;#

I need it to be: abc.com/sites/abcx/000000103483/

But the problem is abcx in the URL ranges from abcx to abcxxxx, where x is an integer.

How can I get the required result?


Solution

  • In PowerShell, you could split on /s and build the string you want like this:

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

    Output:

    abc.com/sites/abcx/000000103483/