Monthly Archive

Categories

PowerShell substrings

PowerShell is all about working with objects but you often have to drop to a lower level and work with properties and their values. Many objects have properties that are strings – a string is one of the standard PowerShell literals – and sometimes you want to extract part of a string – a substring. This post shows how PowerShell substrings work.

First thing to note is that PowerShell doesn’t have a substring command or keyword. A string in PowerShell is an instance of the System.String class and so you use the Substring method of the String class.

You have 2 options – technically known as overloads. You can view a methods overloads by using the method name without brackets

PS> 'abcdefghijklmnopqrstuvwxyz'.Substring

OverloadDefinitions
-------------------
string Substring(int startIndex)
string Substring(int startIndex, int length)

In the first case you give a starting index into the string and the substring starts at that point and takes everything to the end of the string:

PS> 0..25 | foreach {'abcdefghijklmnopqrstuvwxyz'.Substring($psitem)}
abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyz
defghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz
fghijklmnopqrstuvwxyz
ghijklmnopqrstuvwxyz
hijklmnopqrstuvwxyz
ijklmnopqrstuvwxyz
jklmnopqrstuvwxyz
klmnopqrstuvwxyz
lmnopqrstuvwxyz
mnopqrstuvwxyz
nopqrstuvwxyz
opqrstuvwxyz
pqrstuvwxyz
qrstuvwxyz
rstuvwxyz
stuvwxyz
tuvwxyz
uvwxyz
vwxyz
wxyz
xyz
yz
z

Remember that in .NET indices start at 0 so the string is 26 characters long with indices 0-25

If you supply an index that would be beyond the end of the string:

PS> 'abcdefghijklmnopqrstuvwxyz'.Substring(26)

PS> 'abcdefghijklmnopqrstuvwxyz'.Substring(30)
Exception calling "Substring" with "1" argument(s): "startIndex cannot be larger than length of string.
Parameter name: startIndex"
At line:1 char:1
+ 'abcdefghijklmnopqrstuvwxyz'.Substring(30)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

In the first case you get nothing back because the string is 26 characters long. Anything larger than that and you get an error.

The second overload of substring involves supplying a starting index and the number of characters you want to take (including the starting character):

PS> 0..25 | foreach {'abcdefghijklmnopqrstuvwxyz'.Substring($psitem, 3)}
abc
bcd
cde
def
efg
fgh
ghi
hij
ijk
jkl
klm
lmn
mno
nop
opq
pqr
qrs
rst
stu
tuv
uvw
vwx
wxy
xyz
Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string.
Parameter name: length"
At line:1 char:18
+ 0..25 | foreach {'abcdefghijklmnopqrstuvwxyz'.Substring($psitem, 3)}
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string.
Parameter name: length"
At line:1 char:18
+ 0..25 | foreach {'abcdefghijklmnopqrstuvwxyz'.Substring($psitem, 3)}
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Notice the error when you would go beyond the end of the string

PowerShell substrings depend on using the SubString method of the System.String class. The examples here will help you decide which overload to use.

Comments are closed.