如何分割逗号分隔文本文件并将结果存入ListBox控件?
已成功读取并加载CSV数据第一列
测试数据
Name,Surname,Gender,Age Queeneth,Sefoka,Female,33
实现代码
string path = @"c:\temp\Data.txt"; using (StreamReader sr = File.OpenText(path)) { string s; int Pos; string col1,col2,col3,col4; while ((s = sr.ReadLine()) != null) { Pos = s.IndexOf(','); if (Pos != -1) { col1 = s.Substring(0, Pos); listBox1.Items.Add(col1 ); } } }
上述代码通过逐行读取指定路径下的CSV文件,借助IndexOf方法定位每行第一个逗号的位置,截取逗号前的字符串作为第一列数据,最终将这些数据添加到listBox1控件中,已成功完成第一列数据的读取与加载。
内容的提问来源于stack exchange,提问作者Queeneth
相关产品推荐
相关产品推荐

