添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I am [ completely new to PowerShell and ] concatenating a string in a loop, if a special condition occurs I should insert a line break...how can I do this?

Basically looking for the equivalent of \n .

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

So far I have tried

"\n" '\n' "\N" '\N' "\r" '\r' "\R" '\R' '`n' '`r' '-n' '-r' 

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"
                Thanks but doesn't work. it  gives me The term ' ' is not recognized as the name of a cmdlet, function, sc....
                    – user454322
                Aug 9 '12 at 4:52
                The examples in Help about_Escape_character helped me to figure out the problem. Editing answer to show the solution
                    – user454322
                Aug 9 '12 at 4:56
foreach($line in $file){
  if($line -Match $review){ #Special condition
    $str += Write-Host ""
    $str += ANSWER #looking for ANSWER
  #code.....
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.10.7.35131