Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: Remove the last '/' character in a string with multiple paths in it from each path

Tags:

powershell

I'm looking to remove the final '/' character from a string that contains multiple storage paths in it, what is the best way to go about this, I keep getting close but I still can't quite get what I'm looking for, is a loop really the only way?

$Paths = /some/path/1/ /some/path/2/ /some/path/3/
$Paths = $Paths.Remove($Paths.Length - 1)
$Index = $Paths.LastIndexOf('/')
$ID = $Paths.Substring($Index + 1)

I'm currently getting errors like the following:

Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."

The desired final version of $Paths would be

/some/path/1 /some/path/2 /some/path/3

Any help would be greatly appreciated, I think I may have a process issue as well as a coding issue...

like image 479
whoopn Avatar asked Nov 28 '22 22:11

whoopn


1 Answers

Use the .TrimEnd() method.

PS > $Paths = '/some/path/1/','/some/path/2/','/some/path/3/'
PS > $Paths
/some/path/1/
/some/path/2/
/some/path/3/
PS > $Paths = $Paths.TrimEnd('/')
PS > $Paths
/some/path/1
/some/path/2
/some/path/3
like image 130
tommymaynard Avatar answered Dec 06 '22 10:12

tommymaynard