Link to home
Start Free TrialLog in
Avatar of zhshqzyc
zhshqzyc

asked on

Select the first column

Suddenly I am stucked. Shamed.
Select first column in a text file.
 string[] lines = File.ReadAllLines(inputFile);
                           var list1 = from line in lines
                            let coulmns=line.Split('\t')
                            select columns[0]

Open in new window

????
Avatar of for_yan
for_yan
Flag of United States of America image


Perhaps
Split("\s+"); or Split("\\s+");
will work ?
Avatar of zhshqzyc
zhshqzyc

ASKER

Error      1      The name 'columns' does not exist in the current context      
In C# I think you dont write "let"
and you need to declare

string [] columns = line.Split("\\s+");

this is how it works in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MyTry
{
    class MyTry
    {
        static void Main(string[] args)
        {
            Console.WriteLine("My try again");
            string line = "sdda\tsadasd sdfds\t sda";
            string[] columns = Regex.Split(line, "\\s+");
            for (int i = 0; i < columns.Length; i++)
            {

                Console.WriteLine(columns[i]);


            }
        }
    }
}

Open in new window


This is the  output from above code
(My try again is from previous cod - see above,
the rest is from splitting the string)

My try again
sdda
sadasd
sdfds
sda

Open in new window

I meant that I want to get
My
sdda

Open in new window

The first column.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this is even with reading form file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MyTry
{
    class MyTry
    {
        static void Main(string[] args)
        {
          


            string[] lines = System.IO.File.ReadAllLines(@"C:\c_temp\test\lines.txt");
            foreach (string line in lines)
            {
                               string[] columns = Regex.Split(line, "\\s+");
                Console.WriteLine(columns[0]);
            }

          }
    }
}

Open in new window

this is of courese output form the above (that is exactly the gibberuish I tyoes in the fiorst columns of the file for testing):
sdfsd
sdf
sdfds

Open in new window