Any specific text inside a string is called a substring. In the PowerShell, a string consists of a set of elements stored while enclosed in a single or double quote, e.g., “ABC” or ‘ABC.’ Suppose you want to find a specific part of a string where the new string will be the substring of the original string. Using a PowerShell, finding a substring is very easy.
Consider a string, e.g. “121 Baker Street”, and you need to take only 121 from this string. To take this out, you will use the PowerShell SubString () method. A SubString () is a method of having two overload arguments and returning that part of the string. Both the arguments are numerical values having a comma (,) between that separates them. The left numerical value is the point where you want to start your substring, and the right value is the count of the total characters you want to take from the point where you started. Example of finding a substring is:
PS C:\> $string1=”121 Baker Street”
PS C:\> $string1.SubString(0,3)
The output will be:
121
In the above example, we found a substring using a starting and ending point of the string. The question is, what to do when we do not know the ending position of the string? What will be done when you have to find only the last five elements from the string and don’t know the last position? In this case, we use the length command, which will specify the endpoint, and we will deduct any certain number of elements from it. Consider the string “ABCD-1234-WXYZ”, and we need to find the last four characters from it; instead of doing this method
PS C:\> $s1=”ABCD-1234-WXYZ ”
PS C:\> $s1.SubString(0,4)
We will use s1.Length – 4 instead of 0 and not use the ending point of the string, and it will give the output like this
PS C:\> $s1=”ABCD-1234-WXYZ ”
PS C:\> $s1.SubString($s1.Length-4)
OUTPUT:
WXYZ
Not specifying the string’s endpoint, the PowerShell substring method will, by default, go to the last element of the string. The length command, which is the total count of the string minus the count of elements you want to pick from the end, will allow you to select a substring.
Another method to get a substring in PowerShell is to use the Remove () method. In the substring method, we extract a specific part of the string and make it substring:
PS C:\> (“ABCD 123”). SubString (2,4)
The output will be “CD 1”, which shows that it took four elements starting index number 2. But in Remove () method deletes certain elements from the string, and the remaining elements become the substring:
PS C:\> (“ABCD 123”). Remove (2,4)
This will remove “CD 1” from the string.
We can replace any substring within a string with another substring. The method we are is using Replace () command:
PS C:\> (“ABCD 123”). Replace (“ABCD”, “WXYZ”)
The output will be:
WXYZ 123
Similarly, to check if a specific substring I present inside a string, we use Contains () command:
PS C:\> (“ABCD 123”). Contains (“12”)
The output will show TRUE.
If we want to find the position of a substring in a string, we use IndexOf () command:
PS C:\> (“ABCD 123”). IndexOf (“C”)
The output will be 2.
The IndexOf () command helps display all the characters up to that particular character. Using SubString, we can have a starting index and display all the elements till that specified index. An error will be displayed if the specified index goes beyond the length of the string:
PS C:\> $A=”He is a clever boy”
PS C:\> $Index= $A.IndexOf(“r”)
PS C:\> $A.SubString(0,$Index)
The output will be:
He is a cleve