Table of Contents [hide]
Using Replace()
Method
Use the Replace()
method to remove double quotes from a string in PowerShell.
For the above code, we used the Replace()
method to replace doubles quotes with empty space. The Replace()
method took two arguments:
- First was the character or string that we wanted to replace – Double quotes (`")
- Second was the character or string we wanted to replace it with – Empty String
For example, the above code replaced all the occurrences of double quotes within the string with empty space. The order of given arguments to the Replace()
method matters a lot. Note that the Double quotes
was given as the first argument to search in a string, and a Empty space
was given as the second argument to replace with the found match.
Let’s see another example where we removed double quotes by replacing them with single quotes.
Further reading:
Using -replace
Operator
Use the -replace
operator to remove double quotes 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 single quote and an empty space for the above example.
Here, the first variable, $newString1
, replaced double quotes within a string with single quotes, whereas the second variable, $newString2
, replaced double quotes with an empty string.
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.
Consider another example of using a regular expression to remove double quotes in PowerShell:
The above code snippet replaced all double quotes in the specified string with single quotes. Here, the -replace
operator was used with the regular expression [""
] to match the double quotes.
That’s all about how to remove double quotes from String in PowerShell.