Load data to 2D array c#

Posted on 2011-03-01
Last Modified: 2012-05-11
Hello all,
I have included a file.txt that i noticed it has 2 separators the tab and ' ' and i am having problems in loading the columns. that's my code.  I have like 1000 txt file of that type. I can't change them all.
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Collections.Generic;

namespace geneticAlgorithm
    class Program
        static void Main(string[] args)
            String [,] array2D;
            int numberOfLines = 0, numberOfColumns = 0;
            string line;
            String filename = "C:\\Decomposition\\Analyse\\image143\\feature143.txt";
            System.IO.StreamReader sr = new System.IO.StreamReader(filename);
            while((line = sr.ReadLine()) != null)

                numberOfColumns = line.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries).Length;
            array2D = new String[numberOfLines, numberOfColumns];
            numberOfLines = 0;
            sr = new System.IO.StreamReader(filename);
            while ((line = sr.ReadLine()) != null)
                String[] tempArray = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < tempArray.Length; ++i)
                   //array2D[numberOfLines, i] =tempArray[i];

            String Stop;
            Console.Write("press any Key to Stop program");
            Stop = Console.ReadLine();

Open in new window

Question by:dadadude
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Author Comment

ID: 35005849
txt File in here......
LVL 52

Accepted Solution

Carl Tawn earned 500 total points
ID: 35005948
You can pass multiple delimiters to the split method.

numberOfColumns = line.Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries).Length;

Open in new window


Author Closing Comment

ID: 35005983
Thank you. worked great!

Author Comment

ID: 35005991
starting to like c# again. lol

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : All lightning effects with instructions : http://www.mediaf…
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question