简体   繁体   English

所有添加到一个字符串的新行

[英]New line for all strings added to one

How can I add all string to one variable with each in new line I have tried all these options but nothing seems to be working *All variable types here are string so thats not the problem 我如何将所有字符串都添加到一个变量中,并在换行符中尝试了所有这些选项,但似乎无济于事*这里的所有变量类型都是字符串,所以这不是问题

$Body = $alerts[1].description("'n") + $alerts[1].name("'n") + alerts[1].timeadded

$Body = $alerts[1].description "`n" + $alerts[1].name "`n" + $alerts[1].timeadded

$Body = $alerts[1].description `n + $alerts[1].name `n + $alerts[1].timeadded

$Body = $alerts[1].description `n  $alerts[1].name `n $alerts[1].timeadded

I want the output of $body to appear in new line each: 我希望$body的输出每个出现在新行中:

$alerts[1].description
$alerts[1].name
$alerts[1].timeadded

I believe what you're looking for is: 我相信您正在寻找的是:

$Body = $alerts[1].description + "`n" + $alerts[1].name + "`n" + $alerts[1].timeadded

The character `n corresponds to a newline, which should join the strings as you described. 字符`n对应一个换行符,该换行符应按照您的描述连接字符串。

When making multiline strings where formatting is important you can always use a here string and/or the format operator . 在需要格式化的多行字符串时,您始终可以使用here字符串和/或format运算符

$Body = @"
$($alerts[1].description)
$($alerts[1].name)
$($alerts[1].timeadded)
"@

Or 要么

$Body = @"
{0}
{1}
{2}
"@ -f $alerts[1].description, $alerts[1].name, $alerts[1].timeadded

Or 要么

$Body = "{0}`r`n{1}`r`n{2}" -f $alerts[1].description, $alerts[1].name, $alerts[1].timeadded

The features of both those approaches is more apparent when you start adding more information. 当您开始添加更多信息时,这两种方法的功能都更加明显。

$Body = @"
The alert is described as: {0}
It has a name of {1}
This happened at {2} local time.
"@ -f $alerts[1].description, $alerts[1].name, $alerts[1].timeadded

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM