从特定位置读取文本文件

时间:2012-10-25 08:38:14

标签: c# split find text-files readfile

我有一个关于阅读文本文件的问题,因为我不知道我是否正确。我想从特定字符串读取特定字符。

我的文字看起来像这样:

... 
...
CM_ "Hello, how are you?

Rules: Don't smoke!
      - love others

End";
...
CM_ "Why you?";
...// Many CM_
...

Splited之后应该是这样的:

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);
}

5 个答案:

答案 0 :(得分:1)

    static void PRegex()
    {
        using (StreamReader fin = new StreamReader("text.txt"))
        {
            string tmp = fin.ReadToEnd();

            var matches = Regex.Matches(tmp, "(CM_) ([^;]*);", RegexOptions.Singleline);
            for (int i = 0; i < matches.Count; i++)
                if (matches[i].Groups.Count == 3)
                    Console.WriteLine((2 * i + 1).ToString() + ". " + matches[i].Groups[1].Value + "\r\n" + (2 * (i + 1)).ToString() + ". " + matches[i].Groups[2].Value);
        }

        Console.ReadLine();
    }

    static void PLineByLine()
    {
        using (StreamReader fin = new StreamReader("text.txt"))
        {
            int index = 0;
            string line = null;
            string currentCMBlock = null;
            bool endOfBlock = true;
            while ((line = fin.ReadLine()) != null)
            {
                bool endOfLine = false;
                while (!endOfLine)
                {
                    if (endOfBlock)
                    {
                        int startIndex = line.IndexOf("CM_ ");
                        if (startIndex == -1)
                        {
                            endOfLine = true;
                            continue;
                        }
                        line = line.Substring(startIndex + 4, line.Length - startIndex - 4);
                        endOfBlock = false;
                    }

                    if (!endOfBlock)
                    {
                        int startIndex = line.IndexOf(";");
                        if (startIndex == -1)
                        {
                            currentCMBlock += line + "\r\n";
                            endOfLine = true;
                            continue;
                        }
                        currentCMBlock += line.Substring(0, startIndex);
                        if (!string.IsNullOrEmpty(currentCMBlock))
                            Console.WriteLine((++index) + ". CM_\r\n" + (++index) + ". " + currentCMBlock);
                        currentCMBlock = null;
                        line = line.Substring(startIndex + 1, line.Length - startIndex - 1);
                        endOfBlock = true;
                    }
                }
            }
        }

        Console.ReadLine();
    }

答案 1 :(得分:0)

您正在将整个文件读入tmp。因此,如果“CM_”之前有任何文本,则不会输入您的条件语句。

相反,尝试在所有行的循环中逐行读取fin.ReadLine。

答案 2 :(得分:0)

阅读整个文件:

string FileToRead = File.ReadAllText("Path");

string GetContent(string StartAt, string EndAt, bool LastIndex)
{
string ReturnVal;

if(LastIndex)
{
ReturnVal = FileToRead.Remove(FileToRead.IndexOf(StartAt), FileToRead.IndexOf(EndAt));
Return ReturnVal;
}
else
{ 
ReturnVal = FileToRead.Remove(FileToRead.LastIndex(StartAt), FileToRead.LastIndex(EndAt));
Return ReturnVal;
}
}

- 希望我这里没有做错任何事。 (自由心灵打字)

您阅读了该文件,我们删除了第一个索引前面的所有内容。以及之后的一切。 如果将返回找到的FIRST结果,您可以设置它。或者是最后一个。

注意:我认为使用StringReader会更好。 (如果我不记得错了......) 如果您要考虑应用程序的内存使用情况。

答案 3 :(得分:0)

我尝试了别的东西,不知道这是不是很好。它仍然读取第一行,不知道我在这里做错了

我的代码:

        while ((tmp = fin.ReadLine()) != null)
        {
            if (tmp.StartsWith("CM_ "))
            {

                //string[] tmpList = tmp.Split(new Char[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
                var result = tmp.Split(new[] { '"' }).SelectMany((s, i) =>
                {
                    if (i % 2 == 1) return new[] { s };
                    return s.Split(new[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
                }).ToList();

                if (tmp.EndsWith(";")) break;

                fin.ReadLine();

                if (tmp.EndsWith(";"))
                {
                    result.ToList();
                    break;
                }
                else
                {
                    result.ToList();
                    fin.ReadLine();
                }

                foreach (string x in result)
                {
                    Console.WriteLine(x);
                }
            } 

答案 4 :(得分:0)

我建议您研究使用正则表达式。它可能正是你需要的,比Split()更灵活。