Return part of a longer string.

      .Substring( StartIndex [, length] )

   StartIndex  Characters to skip from the start of the string.
               Use 0 to start from the beginning of the string.
               Cannot be less than zero.

   length      The number of characters to return.
Cannot be less than zero or longer than the string. If omitted all remaining characters will be returned.


Skip the first 2 characters and then return the next 3 characters from the string "abcdef":

PS C:\> "abcdef".substring(2,3)

Return the first 5 characters from a string (LEFT):

PS C:\> $var = 'Hello World'
PS C:\> $result = $var.substring(0, 5)
PS C:\> $result

Return the last 5 characters from a string (RIGHT):

PS C:\> $var = 'Hello World'
PS C:\> $length = $var.length
PS C:\> $result = $var.substring($length -5)
PS C:\> $result

“As long as Palestinian violence exists, but not a Palestinian state, Israel is in danger, because it cannot obtain assistance from the international community against an entity that is not subordinate to international law” ~ Stephane Hessel

Related PowerShell Cmdlets:

Replace() - Replace characters within a string.
PowerShell Methods

Copyright © 1999-2022
Some rights reserved