我正在PowerShell中编写一个脚本,它会显示一条很长的消息。基本上,我试图弄清楚如何将字符串分成多行,但它仍然会显示为一行。
$paragraph = "This is a test to create a single-lined string,
but it doesn't seem to work. I would prefer
to make source code easier to read."
预期输出:
This is a test to create a single-lined string, but it doesn't seem to work. I would prefer to make source code easier to read.
实际输出:
This is a test to create a single-lined string.`
but it doesn't seem to work. I would prefer
to make source code easier to read.
我尝试过使用反引号,但这会产生相同的效果。有人知道格式化代码的正确方法吗?
答案 0 :(得分:4)
我会使用here-string和-replace
:
$paragraph = @"
This is a test to create a single-lined string.
But it doesn't seem to work. I would prefer
to make source code easier to read.
"@ -replace "`n"," "
答案 1 :(得分:3)
我会这样做:
$paragraph = "This is a test to create a single-lined string, " +
"but it doesn't seem to work. I would prefer " +
"to make source code easier to read."
答案 2 :(得分:2)
可能有一个更好的解决方案,但过去我已采用这种方法使事情变得可读:
$paragraph = "This is a test to create a single-lined string, "
$paragraph += "but it doesn't seem to work. I would prefer "
$paragraph += "to make source code easier to read."