2

I am really new to PowerShell. I am trying to use .TrimEnd(", ").

How do I have it trim the end only if the characters ", " exist at the end? And to skip otherwise?

I have looked online, but I could not find any information.

1 Answer 1

3

TrimEnd and the other trim methods from the String class will remove any appearance of any of the characters passed as argument so in this case, these methods will not solve your issue. You could definitely use -replace in this case and by doing so there wouldn't be a need to check if the string ends with , just let the regex replacement operator handle that for you.

'string, ' -replace ', $'

See https://regex101.com/r/cLCgC3/1 for details and a more visualized representation of what this means.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi! this would work but I only want to do this at the end of the string. Is there a way to make it so it only replaces the end?
@Jasmine this is exactly doing that. give it a try. $ in regex means end of the line, so it will only target and remove " ," only if it is at the end of the line
@Jasmine, given that Santiago's answer verifiably solves your problem, please consider accepting it or providing feedback, if your question isn't fully answered yet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.