Reputation: 883
Why do I get newlines in the following:
[string[]]$lines= "001 AALTON, Alan .....25 Every Street","002 BROWN, James .... 101 Browns Road","008 FLANAGAN, Lesley .... 1 Lovers Lane"
foreach ($line in $lines) {
$line
}
Output:
001 AALTON, Alan .....25 Every Street
002 BROWN, James .... 101 Browns Road
008 FLANAGAN, Lesley .... 1 Lovers Lane
The foreach docs make no mention of newlines. My loop does not contain newline instruction. And it's obviously not in the source data?
Any explanation would be appreciated.
Upvotes: 0
Views: 1035
Reputation: 437197
The newlines are a formatting artifact, they're not part of your array elements: PowerShell's output formatting system renders each element of a collection of strings / primitive types on its own line, and the same applies to outputting instances of such types one by one to the success output stream, which is what your foreach
statement does.
To give a simple example: 'foo', 'bar'
, 'foo'; 'bar'
and foreach ($s in 'foo', 'bar') { $s }
all result in the same output:
foo
bar
If you need a different representation, create a string in the desired format yourself; e.g.:
PS> $lines -join ', '
001 AALTON, Alan .....25 Every Street, 002 BROWN, James .... 101 Browns Road, 008 FLANAGAN, Lesley .... 1 Lovers Lane
Upvotes: 2