Home > Enterprise >  Azure Devops Split String function
Azure Devops Split String function

Time:04-30

I'm having trouble in an azure devops pipeline.

$(Build.SourceBranch) outputs something like refs/heads/xyz

I'd like to use something like JavaScript's .split('/') to output an array at the slashes.

Meaning $b = $(Build.SourceBranch).Split('/') would output

$b[0] = "refs"
$b[1] = "heads"
$b[2] = "xyz"

I'm not finding much on how I can do this easily.

CodePudding user response:

In Azure Pipeline, you can use PowerShell script to split string.

From your script, you need to add double quotes around the variable:$(Build.SourceBranch) to make it a string first and then split it.

Here is an example:

- powershell: |
   
   $b = "$(Build.SourceBranch)".Split("/")
   
   $b[0]
   $b[1]
   $b[2]
  displayName: 'PowerShell Script'

Result:

enter image description here

  • Related