2014-03-28 122 views
0

當前我正在逐行讀取製表符分隔的文件,拆分每行中的項目並查找包含50個項目的製表符分隔符行硬編碼值50,然後創建 數據表。如何直接讀取製表符分隔的源文件中具有最大製表符分隔符的行

問題:面對 - 製表符分隔的源文件具有製表符分隔的行,有時分割後有50或53個項目。需要直接讀取製表符分隔的源文件中具有最大製表符分隔符的行,然後繼續執行我的邏輯。

我的C#代碼

using (var sr = new StreamReader(sourceFileFullName)) 
{ 
    string line = null; 
    while ((line = sr.ReadLine()) != null) 
    { 
    var items = line.Split(new[] { '\t', '\n' }).ToArray(); 

    if (items.Length != 50) 
    continue; 
    //Rest of my code to create datatable for the items after splitting in tab delimitedline 
    } 
} 

回答

1

你可以使用下面的代碼片段

string content = ""; 
using (var reader = new StreamReader("C:\\temp\\abc.txt")) 
{ 
    content = reader.ReadToEnd(); 
} 
if (!string.IsNullOrEmpty(content)) 
{ 
    var value = content.Split('\n').OrderByDescending(y => y.Split('\t').Count()).Take(1); 
} 
+0

@短跑運動員的變量值指向基於字符串的內容就行了。現在我需要從現在開始一行一行地繼續閱讀並拆分並添加到數據表中。 – Shrivatsan

相關問題