I need to trim path if it ends with \
.
C:\Ravi\
I need to change to
C:\Ravi
I have a case where path will not end with \
(Then it must skip).
I tried with .EndsWith("\")
, but it fails when I have \\
instead of \
.
Can this be done in PowerShell without resorting to conditionals?
To trim whitespace from strings, simply call the trim() method with no arguments like below. When the PowerShell Trim () method is not supplied with any parameters then the Char. IsWhiteSpace method is called and all leading and ending whitespace characters found are removed.
The Split-Path cmdlet returns only the specified part of a path, such as the parent folder, a subfolder, or a file name. It can also get items that are referenced by the split path and tell whether the path is relative or absolute. You can use this cmdlet to get or submit only a selected part of a path.
When you want to extract a part of a string in PowerShell we can use the Substring() method. This method allows us to specify the start and length of the substring that we want to extract from a string.
Consider using TrimEnd
instead (especially if you are working with UNC
Path):
"C:\Ravi\".TrimEnd('\')
no need to overcomplicate
"C:\Ravi\".trim('\')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With