2017-02-28 47 views
1

我是C#的新手。我嘗試使用StreamReader類來分析文本文件:c#streamreader string開頭

1,0 
BRD1,100 - 2017.02.24-12.26 - SaraH 
BRD2,HDF-101D-BL3M,2800,2070,100,0,3,HDF-101D-BL3M,,,0,,0,, 
XBRD1,100 - 2017.02.24-12.26 - SaraH 
XBRD2,100 - 2017.02.24-12.26 - SaraH/0001,2800,270.8,1,0,3,HDF-101D-BL3M,,,0,,1,, 

PNL1,100 - 2017.02.24-12.26 - SaraH 
PNL2,1,HDF-101D-BL3M,1130,295,2,0,,,,,1,0,0,PIL_100_1130x295.bmp 
PRM2,21,0,50,50,0,0,0,80,80,80 
PRM3,18,0,0,15,15,15,75,1,200,2,350,3,650,4,1050,5,0,6,2600,7,4200,8,0,9,0,10,0,11,0,12,0,13,0,14,0,15,0,16,0,17,0,18 

MAT2,HDF-101D-BL3M,HDF 101D white h-3mm,3,0,2,4.8,4.8,4.8,0,0,0,15,15,15,15,5,5,5,0,250,250,0.06,0,60,2200,0,0,0,0,0,1,1,0,0,0,2,0,0,0,1,30,30,30,17,NTMDPI,0,19,9.51,0.03,2,11.59,0.03,2,2,0,0:11,,,,,,,,,,RGB(255:255:255), 

PTN2,1,,1,1,4:38,0:04,5,11,0,0 
PTNR,(((5!),X2),((7!),(9),(9),(9)),(3!,2!)) 

INFO1,100 - 2017.02.24-12.26 - SaraH,100 - 2017.02.24-12.26 - SaraH,standart15,HP30 
INFO4,2 
CHK1,9183 

我需要得到一個字符串有後BRD1, MAT2, INFO4

100 - 2017.02.24-12.26 - SaraH --> to label1 
HDF-101D-BL3M,HDF 101D white-3mm --> to label2 
2         --> to label3 

此刻,我儘量選擇只有正確的線然後分裂。

因爲if (line.Contains(word))選擇所有包含此字符串的行,我需要類似line.BeginWith(word)

此外,如果有人可以幫助我,或者可以介紹我,如果有更好的方法來獲得這個結果。

private void OnChanged(object source, FileSystemEventArgs e) 
{ 
    string line; 
    string[] words = { "BRD1", "MAT2", "INFO4"}; 
    // Read the file and display it line by line. 
    using (var file = new System.IO.StreamReader(e.FullPath)) 
    { 
     while ((line = file.ReadLine()) != null) 
     { 
      foreach (string word in words) 
      { 
       if (line.Contains(word)) 
       { 
        string[] fields = line.Split(','); 
       } 
      } 
     } 
    } 
} 
+2

'line.StartsWith(word)'? – Pikoh

+0

代替分割,你也可以做'var restOfLine = line.Substring(word.Length,line.length - word.length);'這樣你就不依賴於','而且你也不會分裂其餘的的行插入到一個數組中。 – EluciusFTW

回答

1

這將是一個更有效使用regular expressions一次解析全文。

List<string> labels = new List<string>(); 
Regex regex = new Regex(@"\r\n(BRD1|MAT2|INFO4),([^(,|\r\n)]*,?[^(,|\r\n)]*)"); 
using (var file = new System.IO.StreamReader(e.FullPath)) 
{ 
    foreach (Match match in regex.Matches(file.ReadToEnd())) 
    { 
     labels.Add(match.Groups[2].Value); 
    } 
} 
+0

這項工作很好,但在線MAT2它把所有的線,我需要的只是逗號之間的字符串也DF-101D-BL3M,HDF 101D白-3毫米 – nordscan

+0

它會永遠是前兩個「單詞」分隔MAT2之後的逗號? – Aboc

+0

是的,來自所有(BRD1,MAT2,INFO4)我只需要用逗號分隔的「第二個字符串」。就像我上面寫的那樣(標籤1,2,3) – nordscan

1

您可以使用String.StartsWith

foreach (string word in words) 
{ 
    if (line.StartsWith(word)) 
    { 
     string[] fields = line.Split(','); 
    } 
} 

作爲一個附加的簡化,可以避開forach環路與LINQ方法Enumerable.Any

if (words.Any(word => line.StartsWith(word))) 
{ 
    string[] fields = line.Split(','); 
}