Trim()

Remove characters (by default, spaces) from the beginning or end of a string.

Syntax
      .Trim([Characters_to_remove])
      .TrimEnd([Characters_to_remove])
      .TrimStart([Characters_to_remove])

Key
   Characters_to_remove  The characters to remove from the beginning and/or end of the string.
                         Multiple characters can be specified.
                         The order of the characters does not affect the result.

By default trim() will remove leading and trailing spaces and leading and trailing line breaks.
The characters_to_remove may include special characters such as tabs `t, new lines `n or carriage returns `r.

Examples

PS C:\> $result = $myvariable.trim()

Remove spaces from the beginning and end of the string " abcxyz "

PS C:\> Echo " abcxyz ".trim()
abcxyz

Remove characters from the beginning and end of the string "abc xyz"

PS C:\> Echo "abc xyz".trim("xa")
bc xyz
PS C:\> Echo "abc xyz".trim("za")
bc xy
PS C:\> Echo "abc xyz".trim("zay")
bc x

Remove characters from the beginning and end of the string "abc xyz "
PS C:\> Echo "abc xyz ".trim("yaz")
bc xyz


Remove characters from the beginning of the string "abc xyz"

PS C:\> Echo "abc xyz".trimstart("za")
bc xyz

Remove characters from the end of the string "abc xyz"

PS C:\> Echo "abc xyz".trimend("xa")
abc xyz

“Two roads diverged in a wood, and I, I took the one less traveled by,And that has made all the difference” ~ Robert Frost (The Road Not Taken)

Related PowerShell Cmdlets:

PowerShell Methods


 
Copyright © SS64.com 1999-2019
Some rights reserved