简体   繁体   中英

How do I populate an array with foreach?

How do I get the data I can write in the console to write to the array and the console.

At the moment it only displays on the console (not added functionality to add to array).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace TBParser
{
   class Program
   {
    static void Main(string[] args)
    {
        String[] arr = new String[100];
        string[] lines = System.IO.File.ReadAllLines(@"C:\ShpereCompare3.txt");
        Console.WriteLine("Contents of Text File: ");
        foreach (string line in lines)
        {
            Console.WriteLine("\r\t" + line);

        }
        System.IO.File.WriteAllLines(@"C:\Test.txt",lines);
        Console.WriteLine("Press any key to Exit");
        Console.ReadKey();    
    }
   }
}

if my lines of text say

hello
my
name
is
Simon

then the first 5 slots of the array should contain each line?

The line:

string[] lines = System.IO.File.ReadAllLines(@"C:\ShpereCompare3.txt");

is already creating an array, each element of which contains one line.

There is no need to populate a new array with this same information via a foreach .

If you want to copy the lines from the text file into another array, then you can do this:

String[] arr = new String[lines.Length];
Array.Copy(lines, arr, lines.Length);

found a work around. the path i was going down was too complicated. thanks for all your input

fixed code here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace TBParser
{
   class Program
   {
    static void Main(string[] args)
    {
        string fileName = @"C:shpereCompare3.txt";
        List<string> Names = new List<string>();
        List<string> Value = new List<string>();
        using (StreamReader fileReader = new StreamReader(fileName))
        {
            string fileLine;

            while (!fileReader.EndOfStream)
            {
                fileLine = fileReader.ReadLine();
                if (fileLine.StartsWith("Name"))
                {
                    Names.Add(fileLine.Substring(21));
                }
                if (fileLine.StartsWith("Center"))
                {
                    string[] fileSplit = fileLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    Value.Add(fileSplit[1]);
                }
            }
            string outputString = "";
            for (int i = 0;i < Names.Count; i++)
            {
                outputString += Names[i] + " = " + Value[i] + "\r\n";
            }
            System.IO.File.WriteAllText(@"C:Test.txt", outputString);
        }
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM