How to parse a text file with C#

后端 未结 7 1904
面向向阳花
面向向阳花 2020-12-08 14:57

By text formatting I meant something more complicated.

At first I began manually adding the 5000 lines from the text file I\'m asking this question for,into my proje

相关标签:
7条回答
  • 2020-12-08 15:25

    OK, here's what we do: open the file, read it line by line, and split it by tabs. Then we grab the second integer and loop through the rest to find the path.

    StreamReader reader = File.OpenText("filename.txt");
    string line;
    while ((line = reader.ReadLine()) != null) 
    {
        string[] items = line.Split('\t');
        int myInteger = int.Parse(items[1]);   // Here's your integer.
    
        // Now let's find the path.
        string path = null;
        foreach (string item in items) 
        {
            if (item.StartsWith("item\\") && item.EndsWith(".ddj"))
                path = item;
        }
    
        // At this point, `myInteger` and `path` contain the values we want
        // for the current line. We can then store those values or print them,
        // or anything else we like.
    }
    
    0 讨论(0)
提交回复
热议问题