Store everything after second `/`

Viewed 45

I have this string in a Powershell Variable:

$buildParametersSourceBranch = refs/heads/pb/31333-test-branch/name

When I run $buildParametersSourceBranch.split('/')[2]

The result is pb. I assumed the split would show everything after the second / but it seems to only show the value between the second / and third /.

1 Answers

String.Split() doesn't "know" that you're only interested in a particular subset of the result and only want the string split in 2 places.

Use the -split operator and specify a max number of resulting substrings:

$rest = ($buildParametersSourceBranch -split '/',3)[2]
# or
$null,$null,$rest = $buildParametersSourceBranch -split '/',3
Related