[英]Visual C# how to merge two 1D string[] arrays into one 2D string[,] array
I have been diligently studying the help file and all similar questions, but was not able to get to a simpler solution. 我一直在认真研究帮助文件和所有类似的问题,但是无法找到一个更简单的解决方案。 Yet I think there should be.
但是我认为应该有。 I have two
string[]
arrays and I need to merge them into one 2D array. 我有两个
string[]
数组,需要将它们合并为一个2D数组。
Here is the code I got to: 这是我要执行的代码:
public static string[,] GetStructure(string FilePath)
{
try
{
xliff = XDocument.Load(Path.GetFullPath(FilePath));
XNamespace ns = "http://sdl.com/FileTypes/SdlXliff/1.0";
string[] ids = xliff.Descendants().Elements(ns + "tag-defs").Elements(ns + "tag").Elements(ns + "st").Select(e => e.Parent.Attribute("id").Value).ToArray();
string[] elements = xliff.Descendants().Elements(ns + "tag-defs").Elements(ns + "tag").Elements(ns + "st").Select(e => e.Value).ToArray();
string[,] mergedarray = new string[ids.Length, 2];
for (int i = 0; i < ids.Length; i++)
{
mergedarray[i, 0] = ids[i];
mergedarray[i, 1] = elements[i];
}
return mergedarray;
}
catch (Exception)
{
return null;
}
}
Any suggestion to make this merging simpler? 有什么建议可以简化合并吗?
You can use Linq. 您可以使用Linq。
var range = Enumerable.Range(0, ids.Length).ToList();
range.ForEach(i => { mergedarray[i, 0] = ids[i]; mergedarray[i, 1] = elements[i]; });
Maybe there is a better Linq statement 也许有更好的Linq声明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.