2017-02-28 43 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(','); 
}