Question:
I have a replace statement in my code whereby Band’s is being replaced by Cig’s. However when I put single quote it took the first sentence… Example ‘Band’
I tried to use double quote but it does not work. Do you know how to escape the single quote sign?
1 2 |
-replace 'Band's', 'Cig's' |
Answer:
See Escape characters, Delimiters and Quotes and Get-Help about_Quoting_Rules
from the built-in help (as pointed out by as Nacimota).
To include a '
inside a single-quoted string, simply double it up as ''
. (Single-quote literals don’t support any of the other escape characters.)
1 2 |
> "Band's Toothpaste" -replace 'Band''s', 'Cig''s' |
Or, simply switch to double-quotes. (Double-quote literals are required when wishing to use interpolation or escape characters.)
1 2 |
> "Band's Toothpaste" -replace "Band's", "Cig's" |
(Don’t forget that -replace
uses a regular expression)