以#开头的c#streamreader字符串

时间:2017-02-28 12:27:35

标签: c# streamreader

我是C#的新手。我尝试使用StreamReader类解析文本文件:

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 个答案:

答案 0 :(得分:1)

您可以使用String.StartsWith

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

作为额外的简化,您可以使用LINQ方法forach避免Enumerable.Any循环:

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

答案 1 :(得分: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);
    }
}