zhshqzyc
asked on
Select the first column
Suddenly I am stucked. Shamed.
Select first column in a text file.
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]
????
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+");
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]);
}
}
}
}
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
ASKER
I meant that I want to get
My
sdda
The first column.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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]);
}
}
}
}
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
Perhaps
Split("\s+"); or Split("\\s+");
will work ?