如何在 PowerShell 中断行?

我是[ 完全陌生的 PowerShell 和]连接一个字符串在循环中,如果一个特殊的条件发生,我应该 插入一个换行符... 我该怎么做?

基本上就是寻找 \n的等价物。

$str = ""
foreach($line in $file){
if($line -Match $review){ #Special condition
$str += ANSWER #looking for ANSWER
}
#code.....
}

到目前为止我已经试过了

"\n" '\n' "\N" '\N' "\r" '\r' "\R" '\R' '`n' '`r' '-n' '-r'
254001 次浏览

Try "`n" with double quotes. (not single quotes '`n' )

For a complete list of escaping characters see:

Help about_Escape_character

The code should be

$str += "`n"

I think I found it. All you have to do is type in "`n" (WITH THE QUOTATION MARKS!)

Thanks!

Just in case someone else comes across this, to clarify the answer `n is grave accent n, not single tick n

If escaping doesn't work, you can try this:

$str += $("" | Out-String)

It just adds nothing, but as an Out-String, which creates a new line.

You can also just use:

Write-Host "";

Or, to put it in terms of your specific question:

$str = ""
foreach($line in $file){
if($line -Match $review){ #Special condition
$str += Write-Host ""
$str += ANSWER #looking for ANSWER
}
#code.....
}

If you are using just code like this below, you must put just a grave accent at the end of line `.

docker run -d --name rabbitmq `
-p 5672:5672 `
-p 15672:15672 `
--restart=always `
--hostname rabbitmq-master `
-v c:\docker\rabbitmq\data:/var/lib/rabbitmq `
rabbitmq:latest