A problem with trimstart cmdlet

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jramon.r@tv3.cat
Posts: 25
Last visit: Tue Jan 11, 2022 4:23 am

A problem with trimstart cmdlet

Post by jramon.r@tv3.cat »

I have an script with the cmdlet trimstart and I've checked that when the string that I want starts with "S" it trims the "S" too. F.ex. i have

$user='POSPOCPA\Super3'
$user=$user.TrimStart("POSPOCPA\")

The result is $user=uper3

but if I have

$user='POSPOCPA\super3'
$user=$user.trimstart("POSPOCPA\")

then the result is ok $user=Super3

is there any explication for this?

Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: A problem with trimstart cmdlet

Post by jvierra »

This is a little used feature of Windows PowerShell.
This is the current way to enable the feature.

PS C:\scripts> $user='POSPOCPA\Super3'
PS C:\scripts> $user.TrimStart("POsPOCPA\")
SPOCPA\Super3
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: A problem with trimstart cmdlet

Post by jvierra »

If you read the documentation for the "TrimStart" method he answer will become very clear. The issue si the "TrimStart" does not do what you think it does:

https://msdn.microsoft.com/en-us/librar ... .110).aspx
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: A problem with trimstart cmdlet

Post by dan.potter »

('POSPOCPA\Super3').substring(9)
This topic is 8 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked