c# 将锯齿状数组复制到字符串
c# Copying jagged arrays to strings
我的函数应该用“&”“;”分割字符串和 "," 和 return 一个三重锯齿状数组,反之亦然。 (来自以下数据:1,2,3;4,5,6&1,2,3;4,5,6)
我一直在努力让它发挥作用,现在由于某种原因我在
上得到了 system.argumentnullexception
Array.Copy(playerOneEnts, allEnts[0], playerOneEnts.Length);
Array.Copy(playerTwoEnts, allEnts[1], playerTwoEnts.Length);
完整代码:
public string convertToString(string[][][] allEnts)
{
string Player = string.Empty;
string[][] playerOneEnts = new string[maxEnts][];
string[][] playerTwoEnts = new string[maxEnts][];
Array.Copy(allEnts[0], playerOneEnts, allEnts[0].Length);
Array.Copy(allEnts[1], playerTwoEnts, allEnts[1].Length);
for (int j = 0; j < playerOneEnts.Length; j++)
{
for (int i = 0; i < playerOneEnts[j].Length; i++)
{
Player += playerOneEnts[j][i] + ",";
}
Player = Player.TrimEnd(',');
Player += ";";
}
Player = Player.TrimEnd(';');
Player += "&";
for (int j = 0; j < playerTwoEnts.Length; j++)
{
for (int i = 0; i < playerTwoEnts[j].Length; i++)
{
Player += playerTwoEnts[j][i] + ",";
}
Player = Player.TrimEnd(',');
Player += ";";
}
Player = Player.TrimEnd(';');
return Player;
}
public string[][][] convertToArray(string ents)
{
string[] p = new string[2];
string[][] playerOneEnts = new string[maxEnts][];
string[][] playerTwoEnts = new string[maxEnts][];
string[][][] allEnts = new string[2][][];
p = ents.Split('&');
try
{
playerOneEnts = p[0].Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Select(chunk => chunk.Split(',')).ToArray();
playerTwoEnts = p[1].Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Select(chunk => chunk.Split(',')).ToArray();
MessageBox.Show(playerOneEnts.Length.ToString());
Array.Copy(playerOneEnts, allEnts[0], playerOneEnts.Length);
Array.Copy(playerTwoEnts, allEnts[1], playerTwoEnts.Length);
}
catch
{
MessageBox.Show("unable to convert string", "Fatal Error");
}
return allEnts;
}
代码对我来说就像一场灾难,如果有人知道更好的方法将其转换为字符串,我会很高兴提出任何想法。摆脱我的错误已经足够帮助我了。
谢谢!
这是使用 Linq 的示例(需要 .Net 4.0):
var source = "1,2,3;4,5,6&1,2,3;4,5,6";
string[][][] decoded = source.Split('&').Select(x => x.Split(';').Select(y => y.Split(',').ToArray()).ToArray()).ToArray();
string reencoded = String.Join("&", decoded.Select(x => String.Join(";", x.Select(y => String.Join(",", y)))));
或者,可以使用 IEnumarable 泛型来避免转换为数组。
警告:此代码不验证输入,例如数组长度。
编辑: .NET 3.5 重新编码:
var reencoded = String.Join("&", decoded.Select(x => String.Join(";", x.Select(y => String.Join(",", y)).ToArray())).ToArray());
您可以像这样使用嵌套的 for 循环来做到这一点:
string p = "1,2,3;4,5,6&1,5,3;4,5,9";
List<List<List<string>>> result = new List<List<List<string>>>();
foreach (var a in p.Split('&'))
{
List<List<string>> level2 = new List<List<string>>();
foreach (var b in a.Split(';'))
{
level2.Add(new List<string>(b.Split(',')));
}
result.Add(level2);
}
var x = result[0][1][2]; // This will result in '6'.
编码类似:
string encoded;
List<string> dec1 = new List<string>();
foreach (var a in result)
{
string e = "";
List<string> z = new List<string>();
foreach (var b in a)
{
z.Add(String.Join(",", b));
}
e = String.Join(";", z);
dec1.Add(e);
}
encoded = String.Join("&", dec1);
我的函数应该用“&”“;”分割字符串和 "," 和 return 一个三重锯齿状数组,反之亦然。 (来自以下数据:1,2,3;4,5,6&1,2,3;4,5,6)
我一直在努力让它发挥作用,现在由于某种原因我在
上得到了 system.argumentnullexceptionArray.Copy(playerOneEnts, allEnts[0], playerOneEnts.Length);
Array.Copy(playerTwoEnts, allEnts[1], playerTwoEnts.Length);
完整代码:
public string convertToString(string[][][] allEnts)
{
string Player = string.Empty;
string[][] playerOneEnts = new string[maxEnts][];
string[][] playerTwoEnts = new string[maxEnts][];
Array.Copy(allEnts[0], playerOneEnts, allEnts[0].Length);
Array.Copy(allEnts[1], playerTwoEnts, allEnts[1].Length);
for (int j = 0; j < playerOneEnts.Length; j++)
{
for (int i = 0; i < playerOneEnts[j].Length; i++)
{
Player += playerOneEnts[j][i] + ",";
}
Player = Player.TrimEnd(',');
Player += ";";
}
Player = Player.TrimEnd(';');
Player += "&";
for (int j = 0; j < playerTwoEnts.Length; j++)
{
for (int i = 0; i < playerTwoEnts[j].Length; i++)
{
Player += playerTwoEnts[j][i] + ",";
}
Player = Player.TrimEnd(',');
Player += ";";
}
Player = Player.TrimEnd(';');
return Player;
}
public string[][][] convertToArray(string ents)
{
string[] p = new string[2];
string[][] playerOneEnts = new string[maxEnts][];
string[][] playerTwoEnts = new string[maxEnts][];
string[][][] allEnts = new string[2][][];
p = ents.Split('&');
try
{
playerOneEnts = p[0].Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Select(chunk => chunk.Split(',')).ToArray();
playerTwoEnts = p[1].Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Select(chunk => chunk.Split(',')).ToArray();
MessageBox.Show(playerOneEnts.Length.ToString());
Array.Copy(playerOneEnts, allEnts[0], playerOneEnts.Length);
Array.Copy(playerTwoEnts, allEnts[1], playerTwoEnts.Length);
}
catch
{
MessageBox.Show("unable to convert string", "Fatal Error");
}
return allEnts;
}
代码对我来说就像一场灾难,如果有人知道更好的方法将其转换为字符串,我会很高兴提出任何想法。摆脱我的错误已经足够帮助我了。
谢谢!
这是使用 Linq 的示例(需要 .Net 4.0):
var source = "1,2,3;4,5,6&1,2,3;4,5,6";
string[][][] decoded = source.Split('&').Select(x => x.Split(';').Select(y => y.Split(',').ToArray()).ToArray()).ToArray();
string reencoded = String.Join("&", decoded.Select(x => String.Join(";", x.Select(y => String.Join(",", y)))));
或者,可以使用 IEnumarable 泛型来避免转换为数组。
警告:此代码不验证输入,例如数组长度。
编辑: .NET 3.5 重新编码:
var reencoded = String.Join("&", decoded.Select(x => String.Join(";", x.Select(y => String.Join(",", y)).ToArray())).ToArray());
您可以像这样使用嵌套的 for 循环来做到这一点:
string p = "1,2,3;4,5,6&1,5,3;4,5,9";
List<List<List<string>>> result = new List<List<List<string>>>();
foreach (var a in p.Split('&'))
{
List<List<string>> level2 = new List<List<string>>();
foreach (var b in a.Split(';'))
{
level2.Add(new List<string>(b.Split(',')));
}
result.Add(level2);
}
var x = result[0][1][2]; // This will result in '6'.
编码类似:
string encoded;
List<string> dec1 = new List<string>();
foreach (var a in result)
{
string e = "";
List<string> z = new List<string>();
foreach (var b in a)
{
z.Add(String.Join(",", b));
}
e = String.Join(";", z);
dec1.Add(e);
}
encoded = String.Join("&", dec1);