PowerShell Trim String After Character

PowerShell trim string after character

Using Split()Method

Use the Split() method to trim the string after the first occurrence of the specified character in PowerShell.

We declared and initialized the $string variable with "Hi! Welcome to Java2Blog! Let's learn." value. Next, we chained the Split() method with the $string variable to split the $string into an array based on the provided separator. In the above code, the Split() method took the "!" character as an argument and split the $string wherever it found the ! character.

The Split() method returned an array of substrings having three elements; Hi, Welcome to Java2Blog, and Let's learn.. As we were only interested in the first element, we used the index operator by giving 0 as an index ([0]) to grab the Hi element and store it in the $newString variable. Finally, we used the Write-Host cmdlet to print the $newString value on the PowerShell console.

Using IndexOf() and Substring() Methods

Use the IndexOf() and Substring() methods to trim the string after the first occurrence of the given character in PowerShell.

Again, we set the value of the $string variable with "Hi! Welcome to Java2Blog! Let's learn." value. We chained the Substring() method with the $string variable this time. The Substring() method took two arguments: the starting index from where we want to extract the string and the ending index. Remember, the character living at the ending index would not be included in the extracted string.

As we wanted to grab the string from the first character of the $string, we mentioned 0 as starting index. To specify the ending index, we used the IndexOf() method, passed "!" as an argument to it to get the index of its first occurrence in the $string variable.

The Substring() method returned Hi, which we stored in the $newString variable to use further with the Write-Host cmdlet to print its value on the PowerShell console.

Using LastIndexOf() and Substring() Methods

Use the LastIndexOf() and Substring() methods to trim the string after the last occurrence of the specified character in PowerShell.

This code block is similar to the previous one, but we trimmed the $string based on the last occurrence of the specified character (!) in $string. Note that the LastIndexOf() method took one argument, which was "!" and returned the index of the last occurrence of ! in $string.

Using Replace(), Substring(), & IndexOf() Methods

Use Replace(), Substring(), and IndexOf() methods together to trim string after the first occurrence of the given character in PowerShell.

In previous examples, we have already learned about the Substring() and IndexOf() methods. This time, we added another method known as Replace(); this method replaces the first argument with the second one in the string. The point is what to replace and how? Let’s understand it.

After setting the value of the $string variable, we used the Substring() with the IndexOf() method to get the ! Welcome to Java2Blog! Let's learn. substring from $string, which we stored in the $substring variable.

After that, we used the Replace() method, providing $substring as the first argument and an empty string ("") as the second argument. The Replace() method replaced the $substring with an empty string and returned Hi, which we stored in the $newString variable and printed on the console using the Write-Host cmdlet.

Using IndexOf() and Remove() Methods

Use IndexOf() and Remove() methods to trim string after the first occurrence of the given character in PowerShell.

This time we used the IndexOf() method to get the index of the ! character’s first occurrence in the $stringand stored it in the $index variable. Next, we used the Remove() method to remove all characters from the specified $index onwards. As a result, we got Hithat we stored in the $newString variable to print it on the console using the Write-Host cmdlet.

Using Regular Expression with replace operator

Use regular expression with replace operator to trim string after the first occurrence of the given character in PowerShell.

For this example, we used the -replace operator to replace the string that matches the given regular expression with an empty string. In the above example, the "!.*" regex means ! and any character after it, any number of times. We stored the returned string by the -replace operator in the $newString variable and printed it on the PowerShell console using the Write-Host cmdlet.

That’s all about PowerShell trim String after character.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *