如何从文本文件中提取特定字符串并添加到List?

时间:2013-11-05 09:26:59

标签: c#

这是我现在使用的代码:

private void words(string path)
{
    List<string> text = new List<string>();
    var intro = "Video File Name:";
    var words = File.ReadAllLines(path)
        .Where(line => line.StartsWith(intro))
        .Select(line => line.Substring(intro.Length).Trim());   
}

变量路径是文本文件的链接。 这是文本文件的内容格式:

Video File Name: MVI_4523.MOV

Lightning Start Location At Frame: 11     Lightning End Location At Frame: 15
Lightning Start Location At Frame: 6     Lightning End Location At Frame: 15

Video File Name: MVI_4524.MOV

Lightning Start Location At Frame: 15     Lightning End Location At Frame: 19
Lightning Start Location At Frame: 4     Lightning End Location At Frame: 19

我想要做的是将文本文件中的所有视频文件名解析为List<string>,例如列表内容将是:

  

index [0] MVI_4523.MOV

     

index [1] MVI_4524.MOV

然后我想遍历List并将每个索引与我拥有的变量进行比较:如string variable = videoFile

例如:

for (int i = 0; i < videosfilesnames.Length; i++)
{
    if (videosfilesnames[i] == videoFile)
    {
        // Here I want to extract from the text file the lightnings values that are belong to the matched video file name for example if MVI_4524.MOV was == to videoFile so here i want to extract the values from the text file: 15 19 and 4 19
        // Then in the end i need to create/draw point red point on the locations 15 to 19 and 4 to 19
        //  So when extracting the values I need to be able to know that 15 is start and 19 end.
    }
}

1 个答案:

答案 0 :(得分:1)

这不仅仅是你需要提取的视频名称,而是所有数据,例如。

public class Lightening
{
    public Lightening(int start, int end)
    {
        StartLocation = start;
        EndLocation = end;
    }

    public int StartLocation { get; private set; }

    public int EndLocation { get; private set; }
}

public class Video
{
    public Video(string name)
    {
        Name = name;
        Lightenings = new List<Lightening>();
    }

    public string Name { get; private set; }

    public List<Lightening> Lightenings { get; private set; }
}
....
private List<Video> ExtractInfo(string path)
{
    var videos = new List<Video>();
    Video currentVideo = null;
    using (var file = new System.IO.StreamReader(path))
    {
        string line;
        Regex regex = new Regex(@"\d+");
        while((line = file.ReadLine()) != null)
        {
            if (line.StartsWith("Video"))
            {
                currentVideo = new Video(line.Split(':')[1].Trim());
                videos.Add(currentVideo);
            }
            else if (line.StartsWith("Lightning"))
            {
                var matches = regex.Matches(line);
                if (matches.Count == 2 && currentVideo != null)
                {
                    var l = new Lightening(Int32.Parse(matches[0].Value), Int32.Parse(matches[1].Value));
                    currentVideo.Lightenings.Add(l);
                }
            }
        }
    }
    return videos;
}

这为您提供了渲染所需的所有信息

for (int i = 0; i < videos.Count; i++)
{
    if (videos[i].Name == videoFile)
    {
        foreach (var l in videos[i].Lightenings)
        {
            // draw from l.StartLocation to l.EndLocation
        }
    }
}