Table of Contents [hide]
Remove Character from String in PowerShell
Using Replace()
Method
Use the Replace()
method to remove character from a string in PowerShell. Replace()
method replaces old character with new Character in the String.
For the above code, we used the Replace()
method to replace Character a
with empty space
which eventually remove character from String. The Replace()
method took two arguments:
- First was the character or string that we wanted to replace –
a
- Second was the character or string we wanted to replace it with – Empty String
Using -replace
Operator
Use the -replace
operator to remove character from a string in PowerShell.
Here, we used the -replace
operator, similar to the Replace()
method, which took two arguments; the first was the regular expression pattern we wanted to match, and the second was the replacement string. Now replacement string can be anything; we are using a Character a
and an empty space for the above example.
Note: The
-replace
operator works the same way as the.Replace()
method, in which we provide a string to find and replace. However, the-replace
operator has one significant advantage: it allows us to search for matching strings usingregular expressions (regex)
, which is impossible using the.Replace()
method.
Using Remove()
Method
Use the Remove()
method to remove a character from the specified string in PowerShell.
After declaring and initializing the $string
variable, we used the Remove() method, which returned a new string in which the given number of characters are deleted. This method took two parameters; the first was the startIndex
, a zero-based position (because indexes start with 0
) to delete characters. This parameter represented the index of the character that we wanted to delete. The second parameter was the count
, denoting the number of characters to be deleted.
For instance, we used $string.Remove(0,1)
to remove one J
character (from the 0th
index). We can also use it with different arguments based on what we want to accomplish. For example, the $string.Remove(2,1)
will remove v
at index 2
only because the second argument is 1
, representing the number of characters to be removed.
We can use another variation of the Remove()
method but for a different scenario where we want to get a new string in which the current instance’s all characters, starting at the given position till the last position, have been removed. Let’s learn it with the following example.
This Remove()
method took only one parameter, which is startIndex
, which we already learned above. Then, it started deleting characters from the given position (startIndex
), which is 4
in our case, till the last position and displayed Java
as an output.
NOTE: Don’t forget to update the
$string
variable via re-assigning the new value if you want to save it for future use because theRemove()
method does not update the original string.
Using -join
Operator with -split
Operator
Use the -join
operator with the -split
operator to remove a character from the specified string in PowerShell.
Here, we used -join
and -split
operators to manipulate the $string
variable. The -split
was used to split the $string
into an array of substrings. Now, where to split the $string
? It will split the string whenever it encounters the a
character. After performing the split, we got two substrings, J
and v
and 2blog
.
We used ()
around $string -split "a"
to make an array of all substrings. We can use $string.GetType()
as follows to cross-check that we have successfully got an array of strings. Note that this array will not contain the a
character in any of its elements.
Next, we used the -join
operator to join all array elements created using the -split
operator into one string. Again, the resulting string will also not has the a
character. So, in the above example, it took the $string
variable, split it wherever it found a
and joined all the individual parts of the string array to make a new string without the h
character.
Until now, we have learned how to remove a character, multiple instances of a character, the last character of the specified string, or remove everything starting from the given position. But what if we want to keep a chunk of the string and remove everything before and after that specific chunk? In that case, we can use the split()
method.
Further reading:
Remove String between before and after of two characters
We can use split()
method to remove before and after of two characters.
Below code removes everything before =
and after ,
The split() method did the same as the -split
operator that we used in the previous section with the -join
operator. It splits the $string
into substrings based on the given delimiting characters (separators). In the above example, we split the $string
into an array of substrings using =
as a delimiting character and then took the second element of that array using an indexing operator ([1]
), then again, we split the second element of the array using comma (,
) as delimiting character and took the first element of the new array using an indexing operator ([0]
).
So the final output was the first element of the array which was obtained by splitting the second element of the array, which was obtained by splitting the $string
variable using =
as a separator and ,
as a separator, respectively.
Remove Last Character of String in PowerShell
What if we want to delete the last character of the $string
only? In that case, we can use the .Length
property of the $string
variable as follows.
Here, we used the .Length
property to get the size of the value contained by the $string
variable, which is 9
, and saved it in the $length
variable. Then, to get the index of the last character, we subtracted 1
from the $length
because indexes start from 0
, which puts the last character at length-1
. Now, we passed the $length
and 1
as count
to the Remove()
method to delete the last character, which was g
at index 8
in the above code.
That’s all about how to remove character from String in PowerShell.