Powershell: split the line into a new line, then use -contains

$a = {aa
bb
cc}
$a = $a -split "`n"
$a -contains "aa"

This returns false when the string "aa" appears to be on the list. Why is this?

+4
source share
3 answers

To complement Kory Gill’s useful answer , which suggests that the problem may be that there are CRLF ( "`r`n") line endings in the input , in this case -split "`n"(splitting into LF only) will leave the resulting array elements with ending CR ( "`r"), resulting in "aa"not will be found using -contains, since the actual value "aa`r".

Windows Unix :

$a -split '\r?\n' # split $a into lines, whether it has CRLF or LF-only line endings

\r?\n ( ), LF (\n), (?), CR (\r).
\r \n PowerShell `r `n ( ).

, PowerShell , :

  • Windows LF (\n).

  • CRLF (\r\n) LF (\n), PowerShell , .

    • , script , : script - script -block, { } - .

: [Environment]::NewLine , (-) : "`r`n" Windows, "`n" Unix- , , , , , .

+3

, `n , , ` r`n Windows.

:

$a = $a -split "`r`n"
+2

Crossing Solution Platform:

$a = $a -split [System.Environment]::NewLine
0
source

Source: https://habr.com/ru/post/1669645/


All Articles