字符串数组到数组数组

Jas*_*oyd 5 powershell

有时 PowerShell 非常出色,而有时又完全令人沮丧且不直观。它几乎总是一个让我悲伤的数组。

这次我有一个字符串数组。我想将每个字符串拆分为空白,以便最终得到一个字符串数组的数组。我已经尝试过这个:

$data | ForEach-Object { $_.Split(@(), [System.StringSplitOptions]::RemoveEmptyEntries) }
Run Code Online (Sandbox Code Playgroud)

但这只是将所有内容扁平化为一大串字符串,就像SelectManyC# 中一样。我也尝试过这个:

$data | Select-Object { $_.Split(@(), [System.StringSplitOptions]::RemoveEmptyEntries) }
Run Code Online (Sandbox Code Playgroud)

但这给了我一系列PsCustomObject. 我觉得这应该非常容易。我是否遗漏了一些完全明显的东西?

use*_*407 4

您可以放置​​一元逗号(数组)运算符来阻止 PowerShell 枚举Split方法返回的数组:

$data | ForEach-Object { ,$_.Split(@(), [System.StringSplitOptions]::RemoveEmptyEntries) }
Run Code Online (Sandbox Code Playgroud)