PowerShell for Windows Admins

Sep 30 2017   3:30PM GMT

PowerShell Trim

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

PowerShell Trim – no its not a new slimming fad. Trimming is the act of removing characters – usually white space – from the beginning or end of a string.

You have three methods on the String class you can use

Trim
TrimEnd
TrimStart

PS> $str = ” abcdefghijk ”
PS> $str.TrimStart()
abcdefghijk
PS> $str.TrimEnd()
abcdefghijk
PS> $str.Trim()
abcdefghijk

You can also trim specific characters from the beginning or end of a string

PS> $str = “.,:abcdefghijk:,.”

PS> $tc = [char]’.’, [char]’,’, [char]’:’

PS> $str.TrimStart($tc)
abcdefghijk:,.
PS> $str.TrimEnd($tc)
.,:abcdefghijk
PS> $str.Trim($tc)
abcdefghijk

You should now be able to handling any string trimming required in your code

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: