如何使字符串段落更易于阅读源代码
How to make string paragraph easier to read for source code
我正在用 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.
我试过使用反引号,但这会产生相同的结果。有人知道格式化我的代码的正确方法吗?
可能有更好的解决方案,但我过去曾采用这种方法来提高可读性:
$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."
我会这样做:
$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."
我会使用 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"," "
我正在用 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.
我试过使用反引号,但这会产生相同的结果。有人知道格式化我的代码的正确方法吗?
可能有更好的解决方案,但我过去曾采用这种方法来提高可读性:
$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."
我会这样做:
$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."
我会使用 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"," "