c# 读取txt 并拆分成二维数组出错 请指点
我的数据格式如下:
111111 2222222 33333 44444 5555555566666 77777 888888 999999 00000aaaa bbbb ccccc ddddd eeeeefffff ggggg hhhhh jjjjj kkkk
string myfile = mypath + "\\atm.txt"; string[] mytmpstring = File.ReadAllLines(myfile,Encoding.Default); string[] mybh = null; string[] array2 = null; for (int i = 0; i < mytmpstring.Length; i++) { array2 = mytmpstring[i].Split('\t'); for (int j=0; j < array2.Length; j++) { mylist[i][j] = array2[j] ; } }
string myfile = mypath + "\\atm.txt"; string[] mytmpstring = File.ReadAllLines(myfile,Encoding.Default); //string[] mylistf = mytmpstring.Split("\r\n"); string[] mybh = null; for (int i = 0; i < mytmpstring.Length; i++) { foreach (string s in mytmpstring) { mybh = s.Split('\t'); foreach(string t in mybh) { for (int y=0; y < mybh.Length; y++) { mylist[i][y] = t; } } } }
string[] mytmpstring = File.ReadAllLines(myfile, Encoding.Default); string[] mybh = null; string[] array2 = null; int first = mytmpstring.Length; string[][] mylist = new string[first][]; for (int i = 0; i < mytmpstring.Length; i++) { array2 = mytmpstring[i].Split('\t'); mylist[i] = new string[array2.Length]; for (int j = 0; j < array2.Length; j++) { mylist[i][j] = array2[j]; } }
[解决办法]
另外,你的那个数据用
mytmpstring[i].Split('\t');
貌似不妥
换成这个看
array2 = mytmpstring[i].Split(' ');
[解决办法]
你是要二维数组还是嵌套数组,如果是 string[][] 这样的嵌套数组
string[] lines = File.ReadAllLines("路径");string[][] mylist = new string[lines.Length][];for (int i = 0; i < lines.Length; i++){ mylist[i] = lines[i].Split('\t');}