Table of Contents
Using -replace
Operator
Use the -replace
operator to remove spaces from a string in PowerShell.
1 2 3 4 5 |
$string = "John Williamson" $string = $string -replace " ","" $string |
1 2 3 |
JohnWilliamson |
First, we must understand that replacing does not mean removing something but replacing one thing with another. Similarly, we used the -replace
operator to replace all occurrences of whitespace character (" "
) with an empty string (""
), which results in effectively removing spaces from a string in PowerShell.
We used this operator to perform a search-and-replace operation on the specified string. The -replace
operator took two parameters; the first was the pattern/regex to search for, and the second was the replacement.
Remember, this operator does not modify the original variable, so it is ok if you don’t have to keep it for future use; otherwise, don’t forget to re-assign the new value (without spaces) to the original variable. We also re-assigned the updated string to the $string
variable.
Imagine a multiline string containing leading and trailing spaces, double-and-more-spaces, tab symbols, or spacebar symbols. Then we can use the -replace
parameter as given below to remove spaces from the given string.
1 2 3 4 5 |
$string = " John Williamson" $string = $string -replace '(^\s+|\s+$)','' -replace '\s+','' |
1 2 3 |
JohnWilliamson |
For the above script, we removed all whitespace characters from the whole string, whether they are spaces, tabs, newlines, etc. Then, we used the -replace
operator twice and both used regular expression patterns to search for the match and replace.
The first -replace
operator searched for one or multiple whitespace characters at the start (^\s+
) of the string or end (\s+$
) of the string and replaced them with an empty string while the second -replace
operator was responsible for looking one or multiple whitespace characters (\s+
) anywhere within the string and replace them with the empty string.
Now, you might be trying to understand the role of +
and $
in the regex (regular expression); in the (^\s+|\s+$)
pattern, the +
quantifier was used after \s
to match one or multiple whitespace characters at the start or end of the $string
. The $
was used to match the end of the line; it matched the position before the final character of the $string
or before the final line break of a multiline $string
.
Here is an image to understand regular expression (^\s+|\s+$)
better.
On the other hand, in the regular expression pattern '\s+'
for a second -replace
parameter, the +
quantifier was used after \s
to match one or multiple whitespace characters anywhere within the $string
.
It is worth noting that in regular expressions, the ^
character had a special meaning; it is the start-of-line (SOL) anchor; it matched the position at the start of the $string
, or the start of a line in a multiline $string
.
Note that the -replace
parameter is case-insensitive by default, meaning Hello and hello are the same. If you are using this operator to replace a single character, use the -creplace
parameter instead of -replace. The -creplace
parameter will consider Hello and hello as two different strings.
Using replace()
Method
Use the replace()
function to remove spaces from a string in PowerShell.
1 2 3 4 5 6 7 8 |
$string1 = "John Williamson" $string2 = "J o h n W i l l i a m s o n" $string1 = $string1.replace(" ","") $string2 = $string2.replace(" ","") $string1 $string2 |
1 2 3 4 |
JohnWilliamson JohnWilliamson |
The replace()
function is similar to the -replace
operator but only used when removing whitespace characters; it does not work with those strings with tabs, line breaks, etc.
As $string
does not have tabs or line breaks, we used this method to replace all the occurrences of whitespace characters with an empty string. Unfortunately, like the -replace
operator, the replace()
method does not update the original string, and we must re-assign it for future use.
Using Trim()
Method
Use the Trim()
method to remove leading and trailing whitespaces from a string in PowerShell.
1 2 3 4 5 |
$string = " John Williamson " $string = $string.Trim() $string |
1 2 3 |
John Williamson |
Another method to remove whitespaces from the specified string is using the Trim()
method of the System.String
.NET class. In this example, the Trim()
method was called on the $string
variable, and the result was re-assigned to the $string
variable because the Trim()
method does not update the original string. We can also use TrimStart()
and TrimEnd()
to remove whitespaces characters from the start and end of the $string
, respectively.
Further reading:
Using .Split()
Method -join
Parameter
Use the .Split()
method with the -join
parameter to remove spaces from the specified string in PowerShell.
1 2 3 4 5 |
$string = " John Williamson " $string = $string.Split('',[System.StringSplitOptions]::RemoveEmptyEntries) -join '' $string |
1 2 3 |
JohnWilliamson |
The above code split the string into an array of characters with an empty string as a separator, removed any empty entries from the resulting array, and then joined the remaining elements of the array back into a single string. Let’s break the the $string.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
to understand easily:
Split()
– This method was used to split the string into an array, with an empty string as the separator.[System.StringSplitOptions]::RemoveEmptyEntries
– This option was used to remove any empty elements that may be present in the array after the split.-join ''
– This operator was used to join the remaining elements of the array back into a single string. The empty string in the-join ''
operator was used as a separator between the array elements.
This approach will also work on multiline strings having tabs, line breaks, etc. See the following example:
1 2 3 4 5 6 |
$string = " John Williamson " $string = $string.Split('',[System.StringSplitOptions]::RemoveEmptyEntries) -join '' $string |
1 2 3 |
JohnWilliamson |
That’s all how to remove spaces from String in PowerShell.