7

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...

1

3 Answers 3

27

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
Sign up to request clarification or add additional context in comments.

Comments

2

I was working on a function today where I needed to test the $Path switch to see if it ended with a '\' or not. This thread was helpful but I came up with another solution with a simple if statement.

The IF Statement tests the last character of the line by calculating the total length (minus 1 character) and if the last character is not equal to '\', the '\' gets added to the $Path value.

$Path = "C:\SomePath"

if ($Path.Chars($Path.Length - 1) -ne '\')
    {
        $Path = ($Path + '\')
    }

$Path Output = "C:\SomePath\"

To reverse it and remove the '\' is also a simple change using the TrimEnd() method.

$Path = "C:\SomePath\"

if ($Path.Chars($Path.Length - 1) -eq '\')
    {
        $Path = ($Path.TrimEnd('\'))
    }

$Path Output = "C:\SomePath"

2 Comments

The if statements can be simplified as follows: if($path[-1] -ne '\') {$path += '\'} or in reverse if($path[-1] -eq '\') {$path.TrimEnd('\')} In powershell, if you specify an index to a string object, it will index into the chars. Additionally, if you specify a negative index, it will read the chars from right to left. Here, an index of -1 will give you the very last char in the string.
The "to reverse it" is redundant as "C:\SomePath".TrimEnd('\') will not remove any character. TrimEnd() does just that - it removes the specified character(s) from the end of the string, if it/they exist(s).
0

other method, use foreach (or %) and remove last char with substring function:

$Paths = "/some/path/1/", "/some/path/2/", "/some/path/3/"
$Paths | %{$_.Substring(0, $_.length - 1) }

2 Comments

But what if you've got a path like "some/path" for some reason and then it cuts off the "h" and the path becomes incorrect? You should either check, if the last character is a slash, or use the solution of @tommymaynard
Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.