我是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(',');
}
}
}
}
}
'line.StartsWith(word)'? – Pikoh
代替分割,你也可以做'var restOfLine = line.Substring(word.Length,line.length - word.length);'這樣你就不依賴於','而且你也不會分裂其餘的的行插入到一個數組中。 – EluciusFTW