Substring()

Return part of a longer string.

Syntax
      .Substring( StartIndex [, length] )


Key
   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.

Examples

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

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

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

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

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
World

“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
Split()


 
Copyright © 1999-2024 SS64.com
Some rights reserved