日期:2014-05-18  浏览次数:20697 次

如何读取文件里每一行的第一列?
如何读取文件里每一行的第一列?
每列用空格分开,只读取数字开头的行。如:

fenshu:
96.5   68
88       71.5

这样的。

------解决方案--------------------
try
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(@ "C:\test.txt "))
{
String line;
String[] cols;
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex( "\\s{1,} ");
System.Text.RegularExpressions.Regex rnum = new System.Text.RegularExpressions.Regex( "\\d ");

while ((line = sr.ReadLine()) != null)
{
cols = r.Split(line);
if (cols.Length > 0)
{
//判断cols[0]是否为数字
if (rnum.IsMatch(cols[0]))
Console.WriteLine(cols[0]);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}