【问题标题】:Search the for a particular word in text file and Append the entire Line在文本文件中搜索特定单词并附加整行
【发布时间】:2015-02-18 13:04:05
【问题描述】:

我有一个包含 7 列的文本文件。列总是相同的,有时可能是 7 个或更多。我的文本文件是tab delimited。 所以我想知道如何设置对文本文件中所有可用列的搜索并删除整行。

目前,我正在为each columns search and delete the file 编写一个单一函数。我需要一个通用函数来搜索整个文本文件并附加该行

代码:

 public void stack()
    {
        string old;
        string iniPath = Application.StartupPath + "\\list.ini";
        bool isDeleteSectionFound = false;
        List<string> deleteCodeList = new List<string>();
        using (StreamReader sr = File.OpenText(iniPath))
        {
            while ((old = sr.ReadLine()) != null)
            {
                if (old.Trim().Equals("[DELETE]"))
                {
                    isDeleteSectionFound = true;
                }
                if (isDeleteSectionFound && !old.Trim().Equals("[DELETE]"))
                {
                    deleteCodeList.Add(old.Trim());
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        using (var reader = new StreamReader(File.OpenRead(textBox1.Text)))
        {
            //;

            List<string> data = new List<string>();
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split('\t');

                if (values.Contains(deleteCodeList))// getting that error
                    continue;

                //
                data.Add(string.Join("\t", values));

            }
        }
        File.WriteAllText(textBox1.Text, sb.ToString());
    }

【问题讨论】:

    标签: c# string text append text-files


    【解决方案1】:

    这是一个简单的应用程序控制台,它读取文件拆分行并搜索字符串。 如果找到字符串,则继续,否则将该行添加到数组中。

    using System.IO;
    using System.Linq;
    
    static void Main(string[] args)
    {
         var reader = new StreamReader(File.OpenRead(@"C:\test.ini"));
    
            List<string> data = new List<string>();
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split('\t');
    
                if (values.Contains("REMOVE"))
                    continue;
    
                //
                data.Add(string.Join("\t",values));
    
            }
    
            //Inside data you have all the lines without the removed. You can rewrite
            //to other file 
    
    
    }
    

    用于打开和读取文件的 sn-p 取自 question

    测试文件:

    223431  121232112   13122   TEST
    322323  1232332 1233123 2311231 123121
    232323  REMOVE  123121  12123211    121212
    

    【讨论】:

    • 我试过你的代码,它很漂亮..我有一个小疑问..我正在从我的 .ini 文件中添加一个文件列表..我想将该列表与你的代码进行比较。当我给我的代码deleteCodeList 时,它显示一个名为'string[]' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.Queryable.Contains&lt;TSource&gt;(System.Linq.IQueryable&lt;TSource&gt;, TSource)' has some invalid arguments 的错误
    • 尝试使用 System.Linq 添加
    • 在我的代码中我正在搜索一个字符串(“DELETE”)你正在使用一个列表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多