【问题标题】:How to make string paragraph easier to read for source code如何使字符串段落更易于阅读源代码
【发布时间】:2015-05-18 06:50:13
【问题描述】:

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

我尝试过使用反引号,但这会产生相同的结果。有人知道格式化我的代码的正确方法吗?

【问题讨论】:

    标签: powershell formatting


    【解决方案1】:

    我会使用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"," "
    

    【讨论】:

      【解决方案2】:

      我会这样做:

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

      【讨论】:

        【解决方案3】:

        可能有更好的解决方案,但我过去一直采用这种方法来使事情变得可读:

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

        【讨论】: