0

I have a string:

$string = "`nHello`n"

And I want to output the raw string data "`nHello`n". Not it formatted like below:

"

Hello

"

How would I go about doing this?

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
adimnos
  • 1
  • 1
  • 1
    use a string literal (single quotes instead of double): ``'`nHello`n'`` – Santiago Squarzon Jul 17 '23 at 20:57
  • Thank you. How would I go about doing this for an already existing string? – adimnos Jul 17 '23 at 21:02
  • maybe. $string = "`nHello`n" $string -replace "`n","" – Aaron Jul 17 '23 at 21:03
  • 2
    ``$string -replace '\n','`n'`` – Mathias R. Jessen Jul 17 '23 at 21:05
  • Sorry, maybe I should be more specific here. I'm trying to troubleshoot an issue and want to compare raw values. There is a way to do it in python, but I am unable to find a way to do it in powershell. Here's the way in python for reference. http://www.learningaboutelectronics.com/Articles/How-to-print-a-raw-string-in-Python.php#:~:text=To%20create%20a%20raw%20string,the%20string%20exactly%20as%20entered. – adimnos Jul 17 '23 at 21:11
  • $string = "`nHello`n" $string -replace "`n","``n" – Aaron Jul 17 '23 at 21:18
  • 2
    The concept of a raw string only applies to _string literal_ representations, not to actual string _values_; the equivalent of a Python `r'...'` string is just `'...'` in PowerShell. It's still unclear what you're trying to achieve. – mklement0 Jul 17 '23 at 21:36

1 Answers1

0

You could just write ``n to replace `n so, in your script, you could write:

$string = "``nblah``n"
Write-Output $string

Output:

`nblah`n

Or if you want a more r"\nblah\n" python approach you could use single quotes

$string = '`nblah`n'
Write-Output $string

Output:

`nblah`n
kggn
  • 73
  • 1
  • 8