我有一个关于阅读文本文件的问题,因为我不知道我的想法是否正确。我想从特定的字符串读取到特定的字符。
我的文字看起来像这样:
...
...
CM_ "Hello, how are you?
Rules: Don't smoke!
- love others
End";
...
CM_ "Why you?";
...// Many CM_
...
拆分后应如下所示:
1. CM_
2. "Hello, how are you?
Rules: Don't smoke!
- love others
End"
3. CM_
4. "Why you?"
... // many CM_
我想从"CM_"
直到读";"
到目前为止我尝试过的代码:
StreamReader fin = new StreamReader("text.txt");
string tmp = "";
tmp = fin.ReadToEnd();
if (tmp.StartsWith("CM_ ") && tmp.EndWith(";"))
{
var result = tmp.Split(new[] { '"' }).SelectMany((s, i) =>
{
if (i % 2 == 1) return new[] { s };
return s.Split(new[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
}).ToList();
}
foreach (string x in result)
{
Console.WriteLine(x);
}