如何在文本文件中找到一行并将其显示在列表框中

我有一个txt文件,其中列出了姓名,日期和性别,如下所示。我需要找到一个子字符串并显示txt文件中具有特定值的行,例如(“ m”)。有人可以帮我修复我的代码吗

(我的txt文件的外观示例) 1.乔什,21,m 2.麦克,09,m 3. emily,08,f

using (StreamReader sr = new StreamReader("Games.txt"))
                {
                    ListBoxGames.Items.Clear();
                    while ((line = sr.ReadLine()) != null)
                    {
                       str = line.Trim().Split(" , ");
                        String test = chargames.ToString();
                        for (int i = 0; i < str.Length; i++)
                        {
                            if (radioBtnPC.Checked == true)
                            {
                                if (str.Contains("NS"))
                                {
                                    bolShownGames = true;

                                    ListBoxGames.Items.Add(line);
                                }
                            }
评论
  • 钻石泪
    钻石泪 回复

    如果您可以使用Nuget包,并且可以为您提供一种简单的方法,请执行以下操作:

    努吉特:

    DataJuggler.Core.UltimateHelper(.Net框架)

    DataJuggler.UltimateHelper.Core(点网核心)

    using System.IO;
    using DataJuggler.Core.UltimateHelper;
    using DataJuggler.Core.UltimateHelper.Objects;
    

    获取行号方法:

    public int GetLineNumber(string path, string searchText)
    {
        // initial value
        int lineNumber = 0;
    
        // local
        int count = 0;
    
        // if the file exists
        if (File.Exists(path))
        {
            // get the text
            string text = File.ReadAllText(path);
    
            // ensure there is text
            if (TextHelper.Exists(text))
            {
                // Get the lines
                List<TextLine> lines = WordParser.GetTextLines(text);
    
                // If the lines collection exists and has one or more items
                if (ListHelper.HasOneOrMoreItems(lines))
                {
                    // iterate the line numbers
                    // (I thought this was set in WordParser, but I guess not)
                    foreach (TextLine tempLine in lines)
                    {   
                        // Increment the value for count
                        count++;
    
                        // set the lineNumber
                        tempLine.LineNumber = count;
                    }
    
                    // get the line
                    TextLine line = lines.FirstOrDefault(x => x.Text.Contains(searchText));
    
                    // if the line exists
                    if (NullHelper.Exists(line))
                    {
                        // Set the lineNumber
                        lineNumber = line.LineNumber;
                    }
                }
            }               
        }
    
        // return value
        return lineNumber;
    }
    

    然后调用它:

    string filePath = @"C:\Temp\Closing.txt";
    string searchText = "announcements";
    
    // get the lineNumber
    int lineNumber = GetLineNumber(filePath, searchText);
    

    Full Source Code Is Here: https://github.com/DataJuggler/UltimateHelper