【问题标题】:How to convert a file to a Int-Array如何将文件转换为 Int-Array
【发布时间】:2018-02-11 18:19:36
【问题描述】:

我有一个非常基本的程序,它将文件作为输入并再次输出。

    private void GetData_Click(object sender, EventArgs e)
    {
            ReadFile.Filter = "Txt File (*.txt)|*.txt";

            DialogResult result = ReadFile.ShowDialog();
            string file = ReadFile.FileName;
    }

现在我想将此文件转换为 int-Array

.txt 文件如下所示:

1: 100
2: 120
3: 121
4: 323
.
. 
.
94: 400
95: 132
96: 42 
0:
1:
2:

我只需要每行的第二个数字

如何将此类文件转换为数组?

【问题讨论】:

  • 您只需要每行的第二个数字吗?那么00、20、21?还是 0、2、2?
  • 我的意思是我只需要“:”后面的数字

标签: c# arrays arraylist


【解决方案1】:

尝试使用 Linq

   using System.IO;
   using System.Linq;

   ......

   int[] array = File
     .ReadLines(file)
     .Select(line => line.Substring(line.IndexOf(':') + 1))
     .Where(line => !string.IsNullOrWhiteSpace(line))
     .Select(line => int.Parse(line))
     .ToArray();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 2020-04-05
    • 1970-01-01
    • 2011-05-20
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多