2015-05-17 50 views
2

我在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. 

我一直在使用反引号试过了,但是这会产生相同的结果。任何人都知道格式化我的代码的正确方法?

回答

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

有可能是一个更好的解决办法,但我已经走了这种方法在过去把事情可读:

$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." 
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"," "