c# - Reading from file with different line length
问题描述
does anyone know how to read from file to array(container with inheritance) when the line length is different?(I hope language barrier won't make any problems:))
Sportas Skaitymas(Sportas sportas)
{
SportininkasCointainer sportininkai = new SportininkasCointainer();
KomandaContainer komandos = new KomandaContainer();
using (StreamReader reader = new StreamReader("Duomenys.txt"))
{
string line = null;
while ((line = reader.ReadLine()) != null)
{
string[] values;
values = line.Split(';');
string a = values[0];
string b = values[1];
string c = values[2];
string d = values[3];
string e = values[4];
string f = values[5];
string g = values[6];
switch (values.Length)
{
case 7:
Krepsininkas krepsininkas = new Krepsininkas(a, b, c, int.Parse(d), int.Parse(e), int.Parse(f), int.Parse(g));
sportininkai.AddSportinkas(krepsininkas as Krepsininkas);
break;
case 6:
Futbolininkas futbolininkas = new Futbolininkas(a, b, c, int.Parse(d), int.Parse(e), int.Parse(f));
sportininkai.AddSportinkas(futbolininkas as Futbolininkas);
break;
case 4:
Komanda komanda = new Komanda(a, b, c, int.Parse(d));
komandos.AddKomanda(komanda);
break;
}
}
return sportas;
}
I would be very grateful :)
解决方案
You can use:
string a = values.Length > 0 ? values[0] : "";
string b = values.Length > 1 ? values[1] : "";
Here, string is only set when value is present else it will be ""
推荐阅读
- python - 将 matplotlib savefig .png 传输到 dest 文件夹
- python - 如何在数据框中仅选择索引列?
- html - 仅使用溢出-y 滚动 DIV
- php - 如何在 PHP 中的一个元素中编写多个不同的 xml 命名空间
- python - 想用 selenium 在页面上刮一张桌子
- caching - 具有大约 100,000 个条目的重复字典访问的最佳缓存策略
- javascript - Vuejs多个复选框不适用于v-model
- mongodb - mongodb 查询 updateMany :更新集合中所有文档的嵌入式文档列表中的字段
- rust - 转换 Vec
> 到 &[&[u8]] - javascript - 在Vue中使用props时数组元素属性未定义