Powershell

How to get substring in PowerShell?

In PowerShell, a collection of characters enclosed in single or double quotations is referred to as a string. A string consists of several characters and a single variable can hold a complete string. Now if we talk about the substring, it can be a single character or a specific part of a complete string. For instance, “welcome” is a substring of the string “welcome to linuxhint”.

This write-up will provide a comprehensive guide to get a substring in PowerShell and in this regard, it will discuss the below-listed learning outcomes:

So, let’s begin!

How to get a substring in PowerShell?

As we have discussed earlier, a substring is a sub-part of a long/complete string. That’s easy to understand, but the main concern is how to get a substring in PowerShell? Well! In PowerShell, a method named substring() is used to get a substring.

Substring() method in PowerShell

The substring() method takes two parameters, the first one specifies the “starting index” while the other one is “length” which determines the length of the substring (i.e. it specifies how many characters will be returned). The below snippet shows the basic syntax of the substring() method:

substring( StartingIndex, length )

The length parameter is optional however if omitted, the remaining string after the starting index will be returned.

How to use the Substring() method?

In this section, we will learn how to get a substring, to do that, we will consider some examples and implement them in PowerShell ISE:

How to get substring in PowerShell?
In the below-given example program, we will utilize the substring() method to get a specific substring “Welcome”:

> "Welcome to Linuxhint.com".Substring(0, 7)

In the above-given piece of code, we passed “0” and “7” as arguments to the Substring method, consequently, we will get the substring from 0th to 7th index:

The output verified the working of the Substring() method.

How to get a substring placed at last three indexes of a String:
In this example, we will utilize the length() method to get the string’s length:

$string = 'Welcome to linuxhint.com'
$findLength = $string.Length
$output = $string.substring($findLength -3)
$output

In this script, we stored a string in variable $string, next we find the string’s length using string.Length() method, and finally, we utilized the substring method to get a substring of specified length and from the specific index:

This is how we can get a substring of the last three characters of a specific string.

Conclusion

A sub-part of a long/complete string is referred to as a substring and to get a substring the Substring() method is used in PowerShell. The Substring() method can have two arguments, the first one specifies the “initial index” while the other one is “length” which determines the length of the substring. In this write-up, we explained how to get a substring in PowerShell with the help of some relevant examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.