Search code examples
powershellpowershell-4.0

Split a string with powershell to get the first and last element


If you do: git describe --long

you get: 0.3.1-15-g3b885c5

Thats the meaning of the above string:

Tag-CommitDistance-CommitId (http://git-scm.com/docs/git-describe)

How would you split the string to get the first (Tag) and last (CommitId) element?


Solution

  • By using String.split() with the count parameter to manage dashes in the commitid:

    $x = "0.3.1-15-g3b885c5"
    $tag = $x.split("-",3)[0]
    $commitid = $x.split("-",3)[-1]