Using a replace statement in a powershell quote string

I want to run a command

foreach-object {$_ -replace

However, the line I'm trying to work with can be described as follows:

this string "has" quotes

whole line

foreach-object {$_ -replace "this string "has" quotes", "this string "won't have" quotes"}

How to make this quote fill a line using powershell?

+5
source share
3 answers

You can either avoid nested double quotes, for example like this: `"or even better, use single quotes to quote a string, then you won’t need to avoid double quotes, for example:

'this string "has" quotes'

Note: with single quotes you will not get the variable extension in the string.

+5
source

Did you try to double your quotes as an escape character for quotes? i.e.

$a = "This String ""Has"" Quotes"
$a = $a.replace("This String ""Has"" Quotes","this string ""won't have"" quotes. ")
+4
source

You can use double quotes Grave (tilde key) as an escape character. You can do this with any special character in the string.

eg.

$a = "This String `"Has`" Quotes"
$a = $a.replace("This String `"Has`" Quotes","this string won't have quotes. ")
0
source

All Articles