问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;namespace文件输如与转换{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){OpenFileDialogopenFileDialog=newOpenFileDialog();openFileDialog.Filter="文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";openFileDialog.Multiselect=true;if(openFileDialog.ShowDialog(this)==DialogResult.OK){stringmyname=openFileDialog.FileName;try{//CreateaninstanceofStreamReadertoreadfromafile.//TheusingstatementalsoclosestheStreamReader.using(StreamReadersr=newStreamReader(myname)){stringline="";//Readanddisplaylinesfromthefileuntiltheendof//thefileisreached.while(sr.Peek()>-1){line+=sr.ReadLine()+"rn";//加处理代码}textBox1.Text=line;}}catch(Exceptionee){//Lettheuserknowwhatwentwrong.MessageBox.Show(ee.Message);}}}}}我已经把一个txt的文件储存到了line里,输出的形式是C1,2505788.356,406164.803C2,2493391.994,422171.511C3,2480377.792,404904.403C4,2491974.393,390165.454C5,2493038.151,405552.812D1,2499150.552,409331.303D2,2502256.530,401423.371该如何将这样一个字符串中的数字输如到一个[n,2]数组中去呢?
解决方案
解决方案二:
System.Text.Encoding.GetEncoding("gb2312");//指定文件所用的字符集varlines=File.ReadAllLines(myname,System.Text.Encoding.Default);
lines就已经是数组了再varresult=newList<string[]>();foreach(varxinlines)result.Add(x.Split(',').ToArray());
解决方案三:
gb2313的数组区是x吗?
解决方案四:
double[][]data=File.ReadAllLines(文件).Select(x=>x.Split(',').Skip(1).Select(y=>double.Parse(y)).ToArray()).ToArray();