我如何将文本文件转换为int数组列表

问题描述 投票:0回答:5

我有一个包含以下内容的文本文件:

0 0 1 0 3 
0 0 1 1 3 
0 0 1 2 3 
0 0 3 0 1 
0 0 0 1 2 1 1 
0 0 1 0 3 
0 0 1 1 3 
0 0 1 2 3 
0 0 3 0 1 
0 0 1 2 3 
0 0 3 0 1

行之间没有空格,但数字之间有空格。我想从txt文件中读取这些整数,并保存在C#中的int数组列表中。

c# arraylist text-files
5个回答
0
投票
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileToIntList
{
class Program
{
    static void Main(string[] args)
    {
        // Read the file as one string.
        System.IO.StreamReader myFile =
           new System.IO.StreamReader("C:\\Users\\M.M.S.I\\Desktop\\test.txt");
        string myString = myFile.ReadToEnd();

        myFile.Close();

        // Display the file contents.
        //Console.WriteLine(myString);
        char rc = (char)10;
        String[] listLines = myString.Split(rc);
        List<List<int>> listArrays = new List<List<int>>();
        for (int i = 0; i < listLines.Length; i++)
        {
            List<int> array = new List<int>();
            String[] listInts = listLines[i].Split(' ');
            for(int j=0;j<listInts.Length;j++)
            {
                if (listInts[j] != "\r")
                {
                    array.Add(Convert.ToInt32(listInts[j]));
                }
            }
            listArrays.Add(array);
        }


        foreach(List<int> array in listArrays){
            foreach (int i in array)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
        }
        Console.ReadLine();


    }
}
}

1
投票

假设您的字符串称为text,并且包含“ 1 1 1 0 3 2 3”等。您声明一个字符串数组。

String[] numbers1=text.Split(" ");

现在声明您的int数组并将每个数组转换为int。

int[] numbers2=new int[numbers.Length];
for(int i=0; i<numbers.Length; i++)
    numbers2[i]=Convert.ToInt32(numbers1[i]);

您已经完成。


1
投票

这对我有用:

var numbers =
    System.IO.File
        .ReadAllLines(@"C:\text.txt")
        .Select(x => x.Split(' ')
            .Select(y => int.Parse(y))
            .ToArray())
        .ToList();

我得到这个结果:

“结果”


0
投票
var resultList = new List<int>();
File.ReadAllLines("filepath") 
    .ToList()
    .ForEach((line) => {
                            var numbers = line.Split()
                                              .Select(c => Convert.ToInt32(c));
                            resultList.AddRange(numbers); 
                        });

0
投票
using System.IO;
using System.Linq;

string filePath = @"D:\Path\To\The\File.txt";
List<int[]> listOfArrays =
    File.ReadLines(path)
    .Select(line => line.Split(' ').Select(s => int.Parse(s)).ToArray())
    .ToList();

由于您提到这是您第一次使用C#编程,因此此版本可能更易于理解;该过程与以上相同:

using System.IO;
using System.Linq;

string filePath = @"D:\Path\To\The\File.txt";

IEnumerable<string> linesFromFile = File.ReadLines(path);

Func<string, int[]> convertStringToIntArray =
    line => {
        var stringArray = line.Split(' ');
        var intSequence = stringArray.Select(s => int.Parse(s)); // or ....Select(Convert.ToInt32);
        var intArray = intSequance.ToArray();
        return intArray;
    };

IEnumerable<int[]> sequenceOfIntArrays = linesFromFile.Select(convertStringToIntArray);

List<int[]> listOfInfArrays = sequenceOfIntArrays.ToList();
© www.soinside.com 2019 - 2024. All rights reserved.