Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:03:13+00:00 2026-06-10T11:03:13+00:00

Using C# (VS 2010 Express) I read the contents of a text file into

  • 0

Using C# (VS 2010 Express) I read the contents of a text file into a string. The string is rather long but reliably broken up by “\t” for tabs and “\r\n” for carriage returns/newlines.

The tabs indicate a new column of data, and new line indicates a new row of data.

I want to create an array or List of dimensions (X)(Y) such that each spot in the array can hold 1 row of data from the text file, and all of the Y columns contained in that 1 row (“\t” means a new column of data, and “\r\n” means a new row of data”).

To make things simple let’s say my text has 10 rows of data, and 2 columns. I’d like to create an array or List or whatever you think is best to store the data. How do I do this? Thanks.

This is the code that I used to read the data in the text file into a string:

// Read the file as one string.
            System.IO.StreamReader myFile = new System.IO.StreamReader("f:\\data.txt");
            string myString = myFile.ReadToEnd();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:03:14+00:00Added an answer on June 10, 2026 at 11:03 am

    Just as is (you already have a string with everything):

    str.Split(new string[]{"\r\n"}, StringSplitOptions.None)
      .Select(s => s.Split('\t'));
    

    Gives you an IEnumerable<string[]> producing variantes like list of list, array of array and so on just needs the suitable ToArray() or ToList() etc.

    However, if you can deal with each line one at a time, you can be better off with something that lets you do so:

    public IEnumerable<string[]> ReadTSV(TextReader tr)
    {
      using(tr)
        for(string line = tr.ReadLine(); line != null; line = tr.ReadLine())
          yield return line.Split('\t');
    }
    

    Then you only use as much memory as each line needs. We could go further and change the reading to emit each individual cell one at a time, but this is normally enough to read files of several hundred MB in size, with reasonable efficiency.

    Edit based on comments on question:

    If you really wanted to, you could get a List<string[]> from:

    var myFile = new StreamReader("f:\\data.txt");
    var list = ReadTSV(myFile).ToList();
    

    Alternatively, change the line yield return line.Split('\t'); to yield return line.Split('\t'); and you get a List<List<string>>.

    However, if possible then work on the results directly, rather than putting it into a list first:

    var myFile = new StreamReader("f:\\data.txt");
    var chunks = ReadTSV(myFile);
    foreach(var chunk in chunks)
    {
       DoSometingOnAChunk(chunk[0], chunk[1]);
    }
    

    It’ll use less memory, and get started faster rather than pausing to read the whole thing first. Code like this can merrily work its way through gigabytes without complaint.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using C# 2010 Express. I have created a SQL Server CE database file
I'm using Visual C# 2010 Express (final, not beta or anything) and I'm having
using Visual.Web.Developer.2010.Express; using SQL.Server.Management.Studio.2008.R2; What I'm ultimately trying to do is update a sql
using Visual.Web.Developer.2010.Express; using SQL.Server.Management.Studio.2008.R2; Kinda new at C# here.. I got the first row
I am using Visual C# Studio 2010 Express and I have oakward problem. I
I am currently using MS Visual Web developer 2010 Express to build a website
I'm using VS Team System 2008 and gonna install VS 2010 Express editions.
I am using visual c# express 2010. I need an installer that will allow
I am using IIS Express in Visual Studio 2010 and right now it runs
So I'm using C# 2010 Express and wondering what the best toolset is for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.