Get Column Index with Name Using StreamReader

Winston TheFifth 106 Reputation points
2022-07-14T23:17:02.693+00:00

How do you get the column index with the column name with a StreamReader?

int column_index = . . .
string column_name;

using( StreamReader sr = new StreamReader( "MyFile.csv"))
{
string line = sr.ReadLine( );
var a = line.Split( ",")
var index = line.IndexOf("ColumnName");
}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,487 questions
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-07-15T05:16:36.713+00:00

    @Winston TheFifth , Welcome to Micosoft Q&A, you could try the following code to use List<T>.IndexOf Method to get the index based on the columnname.

        string column_name="Age";  
        using (StreamReader sr = new StreamReader(path))  
        {  
            string line = sr.ReadLine();  
            var a = line.Split(',');  
            int index=a.ToList().IndexOf(column_name);  
            Console.WriteLine("index is "+index);  
    
        }  
    

    Csv and tested result:

    220965-image.png

    Hope this could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

  2. gjdjgjdsg 0 Reputation points
    2025-05-22T05:34:44.98+00:00

    internal class Auto

        {

            public string Gyarto;

            public string Tipus;

            public string Szin;

            public int Ar;

            // 1. Feladat

            public Auto(string gyarto, string tipus, string szin, string ar)

            {

                this.Gyarto = gyarto;

                this.Tipus = tipus;

                this.Szin = szin;

                this.Ar = int.Parse(ar);

            }

        }

        internal class Program

        {

            static void Main(string[] args)

            {

                #region 1. Feladat

                // a) alfeladat fentebb

                #region b) alfeladat

                StreamReader sr = null;

                List<Auto> autok = new List<Auto>();

                #region c) alfeladat

                try

                {

                    sr = new StreamReader("autok.csv");

                    sr.ReadLine(); //Fejléc sor

                    while (!sr.EndOfStream)

                    {

                        string[] sor = sr.ReadLine().Split(';');

                        autok.Add(new Auto(sor[0], sor[1], sor[2], sor[3]));

                    }

                }

                finally

                {

                    if (sr == null)

                    {

                        Console.WriteLine("A fájlbeolvasás nem sikerült.");

                    }

                    else

                    {

                        sr.Close();

                    }

                }

                #endregion

                #endregion

                #endregion

                #region 2. Feladat

                Console.WriteLine($"\n2. Feladat \nA lista {autok.Count} elemet tartalmaz.");

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.