I currently have code that looks like this:
$onestr = $twostr -replace "one","two" -replace "uno","dos"
I would like to format it like this:
$onestr = $twostr -replace "one","two"
-replace "uno","dos"
such that the replace statements stack on top of each other.
I could use backtic as the line continuation character, but other stackoverflow questions cover why that is not a good idea.
I tried code that looks like this:
$onestr = ($twostr -replace "one","two"
-replace "uno","dos"
)
But I got an error that the paren is not matched.
My actual code has several replace statements (not just two).
If you have a lot of replacements, what about a different approach which allows much more scope for the aligning of the replacement pairs nicely, without affecting the block of replace code.
$onestr = 'one thing uno thing'
$Pairs = @{
'one' = 'two'
'uno' = 'dos'
}
$Pairs.GetEnumerator() | ForEach-Object {
$onestr = $onestr -replace $_.Name, $_.Value
}
$onestr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With