question

TerenceLau-5387 avatar image
0 Votes"
TerenceLau-5387 asked IanXue-MSFT answered

TrimEnd Query

Hi,

I'm trying to trim off the file extension from a filename like this:

$FileName = "C:\Program Files\Microsoft\Configuration Manager\Configuration Manager Console.exe"
$NewName = $FileName.Split("\")[-1].TrimEnd(".exe")

Result is ...

Configuration Manager Consol

... with the "e" at the end missing.

There are a couple of workarounds to fix this including using "-replace" or "Split" it again via the dot. But I would like to understand how TrimEnd works as I was under the impression that only ".exe" would be remove from the end of the string. But it also removes any similar characters before the dot if these characters are "e" or "x" or any combination of them.

Something to do with the way it interprets the characters before and after the dot. Any advice would be most appreciated.

Thanks.

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

The TrimEnd method does not match the string ”.exe“, but matches the characters in the set ".ex".

The TrimEnd(System.Char[]) method removes from the current string all trailing characters that are in the trimChars parameter. The trim operation stops when the first character that is not in trimChars is encountered at the end of the string.

https://docs.microsoft.com/en-us/dotnet/api/system.string.trimend

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.