问题描述
所以这就是了,
我有一个这种格式的文本框:
1,悟空
2,Krillin
3,Friezer
等...
我有一个这样的数组:
array [,] arrDBZ;
我希望文本文件中的每一行都填充我的数组,例如:
arrDBZ会有这个值:
arrDBZ [1,悟空]
arrDBZ [2,Krillin]
arrDBZ [3,Friezer]
等......
我该怎么做?
到目前为止,我有这个代码,我找到了google:
So here's the thing,
I have a Textbox with this format:
1,Goku
2,Krillin
3,Friezer
and so on...
and I have an array like this:
array[,] arrDBZ;
I want that for every line in the text file populate my array for example:
arrDBZ would have this values:
arrDBZ[1,Goku]
arrDBZ[2,Krillin]
arrDBZ[3,Friezer]
and so on...
How can i do this?
So far I have this code I found on google:
using (OpenFileDialog fdText=new OpenFileDialog())
{
fdText.Filter="Text Files (*.txt)|*.txt";
fdText.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
fdText.Multiselect = false;
fdText.Title = "SELECT TEXT FILE";
if (fdText.ShowDialog() == DialogResult.OK)
{
string data = System.IO.File.ReadAllText(fdText.FileName);
string[,] parsedData;
var res = data.Split(',')
.Select(p => Regex.Split(p, "(?<=\\G.{2})"))
.ToArray();
var twoD = new String[res.Length, res[0].Length];
for (int i = 0; i != res.Length; i++)
for (int j = 0; j != res[0].Length; j++)
twoD[i, j] = res[i][j];
txtLoc.Text = fdText.FileName;
parsedData = twoD;
}
但这只填充第二栏:
arrDBZ [0,0]将是arrDBZ [null,Goku],arrDBZ [null,Krillin]等......
我希望有人可以帮我这个。
谢谢。
But this only populate the second column:
arrDBZ[0,0] would be arrDBZ[null,Goku], arrDBZ[null,Krillin] and so on...
I hope someone can help me with this.
Thanks.
推荐答案
这篇关于如何使用文本框填充2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!