c# - 将文本文件加载到列表框中。代码行需要拆分
问题描述
我试图使用 openfiledialog 加载文本文件。string string bool 姓名,高中一年,失败?文本文件中的代码
Chris | Junior | False
Jake Peters | Freshman | True
Jasmine Baker | Senior | False
所以我使用一个数组来获取代码。然后创建一个 foreach 循环以添加到字符串列表
然后使用 for 循环将行拆分为“|”
然后创建一个新对象并使用对象的变量保存数据。
最后添加到列表框
不需要转换。你可以忽略布尔值。我想更了解这个概念
我的意思是我尝试过的方法。根本没有加载。
string[] b = File.ReadAllLines(filePath);
List<string> ...
foreach(var a in b)
{
list.Add(a);
}
for (int i =0; i<students.Count; i++)
{
list[i].Split('|');
//Human object...
Human x = new Human();
x.name =
listbox.add(x);
failinglistbox.Items.Add(x);
解决方案
我想这就是你想要的。
string[] students = File.ReadAllLines(filePath);
for (int i = 0; i < students.Length; i++)
{
string[] cells = students[i].Split('|');
//Human object...
Human x = new Human();
x.name =cells[0].Trim();
////////edited////////////
x.year =cells[1].Trim();
x.failing =cells[2].Trim();
listbox.add(x);
failinglistbox.Items.Add(x);
}
推荐阅读
- windows - 在 Ansible 中,您如何正确地遍历文件以查找块内的任务?
- bash - Bash:防止部分脚本作为 sudo 运行
- flutter - Hive 加密框重复代码,重构导致错误
- spring-boot - 使用无服务器 Java 容器时如何向 lambda 抛出 springboot 异常
- r - 将 Amadeus 航班优惠限制为特定航空公司
- google-sheets - 01d 04h 19m 23s 在谷歌表格上转换为十进制
- python - 如何使用python中的平移矩阵将小图像的特定坐标放置到大图像的特定坐标
- android - 有什么方法可以让我远程帮助我设置开发环境?
- git - 为什么提交一个文件,但所有文件都在 RStudio 中提交?
- c++ - 前向定义类的类型转换重载