且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PowerShell 将字符串拆分为二维数组

更新时间:2022-11-14 17:09:59

好吧,我讨厌自己回答,但这比我想象的要简单得多......其他两个答案都是有效的,Mathias 指出了一个明显的疏忽.

Ok I hate to self answer, but this was far more simple than I thought... Both other answers are valid, and Mathias pointed me to an obvious oversight.

$string = $string -split "," $string = $string.split(",") 相同> 正如马蒂亚斯所说:

$string = $string -split "," is not the same as $string = $string.split(",") As Mathias said:

-split 返回一个 string[] 类型的对象,这意味着你不能将字符串以外的任何内容分配给单个项目而不更改整个数组的类型.

-split returns an object of type string[], meaning that you can't assign anything other than a string to a single item without changing the type of the entire array.

所以我最终使用了:

$services= $services.split("`r`n")

然后

$services[0]= $services[0].split(",")

然后我能够使用 services[0][0]

此外,Bacon Bit 的回答很可能更好;我的回答只是以最简单的方式解决了我的问题.

Also, Bacon Bit's answer is in all likelihood better than my answer; my answer just addresses my question in the simplest possible fashion.