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.

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 Methods


© Copyright SS64.com 1999-2013
Some rights reserved