gpt4 book ai didi

c# - 使用 linq 将字符串转换为 3D 数组

转载 作者:太空宇宙 更新时间:2023-11-03 21:06:45 26 4
gpt4 key购买 nike

我看到了例子,并实现了 linq 来将定界字符串转换为二维数组,如下所示:

using System;
using System.Linq;

namespace AACOBusinessModel.Extensions
{
[Serializable]
public class TwoDimensionalStringArray
{
public TwoDimensionalStringArray(string data)
{
// 1;2;3^^4;5;6
this.Data = (
from string line
in data.Split(new string[] { "^^" }, StringSplitOptions.RemoveEmptyEntries)
select line.Split(';')
).ToArray();
}

public TwoDimensionalStringArray() { }

public readonly string[][] Data = { };

public override string ToString()
{
return string.Join("^^",
from string[] line
in Data
select string.Join(";", line)
);
}
}
}

当我尝试以 3D 形式制作此作品时,我遇到了一个问题:

using System;
using System.Linq;

namespace AACOBusinessModel.Extensions
{
[Serializable]
public class ThreeDimensionalStringArray
{
public ThreeDimensionalStringArray(string data)
{
// 1;2;3^^4;5;6@4;4;4^^7;7;7
this.Data = (
from string line2D
in data.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries)
from string line
in line2D.Split(new string[] { "^^" }, StringSplitOptions.RemoveEmptyEntries)
select line.Split(';')
);
}

public ThreeDimensionalStringArray() { }

public readonly string[][][] Data = { };

public override string ToString()
{
return string.Join("@",
from string[][] line2D
in Data
select string.Join("^^",
from string[] line
in line2D
select string.Join(";", line)
)
);
}
}
}

我知道对于 2D,结束选择是转换为数组的拆分集合,从而形成一个 2D 数组。

但是对于 3D,结束选择需要选择二维数组的集合。我不知道该怎么做。

更新

我想到了这个,但如果可能的话,我想要查询语法。

public ThreeDimensionalStringArray(string data)
{
this.Data = (
from string line2D
in data.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries)
select (
from string line
in line2D.Split(new string[] { "^^" }, StringSplitOptions.RemoveEmptyEntries)
select line.Split(';')
).ToArray()
).ToArray();
}

最佳答案

不确定在查询语法中应该如何完成,但这里是方法语法版本:

this.Data = data
.Split(new[] { "@" }, StringSplitOptions.RemoveEmptyEntries)
.Select(table => table.Split(new[] { "^^" }, StringSplitOptions.RemoveEmptyEntries)
.Select(row => row.Split(';'))
.ToArray())
.ToArray();

在查询语法中:

this.Data = (
from table in data.Split(new[] { "@" }, StringSplitOptions.RemoveEmptyEntries)
select
(
from row in table.Split(new[] { "^^" }, StringSplitOptions.RemoveEmptyEntries)
select row.Split(';')
).ToArray()
).ToArray();

关于c# - 使用 linq 将字符串转换为 3D 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40751593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com