You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何分割逗号分隔文本文件并将结果存入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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 03:22:12