StreamReader行和行分隔符

时间:2015-05-12 19:56:38

标签: c# arrays tokenize delimiter streamreader

我试图找出如何标记文本文件的StreamReader。我已经能够分隔线,但现在我试图弄清楚如何通过制表符分隔符来分解这些线。这是我到目前为止所做的。

string readContents;
using (StreamReader streamReader = new StreamReader(@"File.txt"))
{
    readContents = streamReader.ReadToEnd();
    string[] lines = readContents.Split('\r');
    foreach (string s in lines)
    {
        Console.WriteLine(s);
    }
}
Console.ReadLine();

2 个答案:

答案 0 :(得分:2)

只需在每一行上调用Split()并将其保存在列表中。如果您需要阵列,可以随时拨打列表中的ToArray()

string readContents;
using (StreamReader streamReader = new StreamReader(@"File.txt"))
{
    readContents = streamReader.ReadToEnd();
    string[] lines = readContents.Split('\r');
    List<string> pieces = new List<string>();
    foreach (string s in lines)
    {
        pieces.AddRange(s.Split('\t'));
        Console.WriteLine(s);
    }
}
Console.ReadLine();

答案 1 :(得分:2)

string readContents;
using (StreamReader streamReader = new StreamReader(@"File.txt"))
{
    readContents = streamReader.ReadToEnd();
    string[] lines = readContents.Split('\r');
    foreach (string s in lines)
    {
         string[] lines2 = s.Split('\t');
         foreach (string s2 in lines2)
         {
             Console.WriteLine(s2);
         }
    }
}
Console.ReadLine();

不确定这是否是你想要的,但是......它打破了(制表符)已经破损(返回)的行