本文介绍了元组列表到多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将元组"列表转换为字符串[,]?
How would I go about converting a list of 'Tuple's to string[,]?
这是Web爬网程序的一部分,但是出于好奇,我搞砸了列表和数组的转换.这是方法.
This is part of a web crawler, but i'm messing with conversions of lists and arrays just out of curiosity. Here's the method.
private string[,] getimages(string url)
{
List<Tuple<string, string>> images = new List<Tuple<string, string>>();
string raw = client.DownloadString(url);
while (raw.Contains("<a class=\"title \" href"))
{
raw = raw.Substring(raw.IndexOf("<a class=\"title \" href"));
String link = raw.Substring(24, raw.IndexOf(">", 24) - 26);
int startname = raw.IndexOf(">", 24) + 1;
int endname = raw.IndexOf("</a> ");
String name = raw.Substring(startname, endname - startname);
images.Add(new Tuple<string, string>(name, link));
raw = raw.Substring(endname);
}
}
我想返回图像",但转换为多维数组.
I want to return 'images', but converted to a multidimensional array.
推荐答案
一种简单直接的方法是仅for
列表:
A simple and straight-forward way is to just for
the list:
string[,] result = new string[images.Count, 2];
for(int i=0; i<images.Count; i++)
{
var tuple = images[i];
result[i,0] = tuple.Item1;
result[i,1] = tuple.Item2;
}
return result;
这篇关于元组列表到多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!