【问题标题】:First column names is not deleting from my text file [closed]第一列名称没有从我的文本文件中删除[关闭]
【发布时间】:2014-11-20 23:59:12
【问题描述】:

我正在根据我的 .ini 文件删除一些名称。我的 .ini 文件 中列出了一些名称。每次我的应用程序处理输入文件时,它都会从 .ini 文件中查找某些关键字,如果在我的输入文件中找到该关键字,它会删除整行。

但对我来说,发生的事情是当 .ini 文件在我的文本文件中找到关键搜索 && 如果我的关键搜索在我的第二列中,那么它只会删除该行。我希望关键字搜索也在我的文本文件中查找我的第一列。这个怎么加??

代码sn-p:

public void do_name()
    {
        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 (StreamReader sr = File.OpenText(textBox1.Text))
        {
            while ((old = sr.ReadLine()) != null)
            {
                var st = old.Trim().Split(new char[] { '\t' });
                if (st.Length > 1)
                {
                    var tempCode = st[1].Substring(1, st[1].Length - 2);
                    if (!deleteCodeList.Contains(tempCode))
                    {
                        sb.AppendLine(old);
                    }
                }
                else if (st.Length == 1)
                {
                    //old = "\n";
                    sb.AppendLine(old);

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

我的输入文本文件:在这里你可以看到,第一列的值由于某些原因没有被搜索到。不知道。。

  Designator    MAX PN  Footprint   Center-X(mm)    Center-Y(mm)

   "C10"    "100-0009"  "1206 - CAPACITOR"  "122.492"   "69.469" 
   "C100"   "100-0009"  "1206 - CAPACITOR"  "264.211"   "12.814"
   "C102"   "100-0009"  "1206 - CAPACITOR"  "251.346"   "11.201"

I would like to add C10, C100 and all first column elements to my .ini file

我的 .ini 文件看起来像

 [DELETE]
 100-2333
 233-3233
 C10

【问题讨论】:

    标签: c# string visual-studio-2010 text ini


    【解决方案1】:

    看起来您只查看“1”列。由于数组索引为零,这实际上是您的第二列。以下是如何获取第 1 列和第 2 列的示例,替换您的 while 循环中的代码:

                var st = old.Trim().Split(new char[] { '\t' });
                bool hasDeleteMatch = false;
                //loop through all the columns starting at 0 and up to 1 (inclusive)
                for (int col = 0; col < st.Length && col<=1; col++) 
                {
                    if (st[col].Length>2)
                    {
                      var tempCode = st[col].Substring(1, st[col].Length - 2);
                      if (!deleteCodeList.Contains(tempCode))
                      {
                          hasDeleteMatch = true; //we found a match, don't append to the new file
                          break;
                      }
                    }
                }
                if (!hasDeleteMatch) sb.AppendLine(old);
    

    如果您想将其扩展到更多列,请将 col&lt;=1 更改为更大的数字或将其删除以搜索所有列。

    【讨论】:

    • 出现错误:- Additional information: startIndex cannot be larger than length of string.
    • 在线var tempCode = st[col].Substring(1, st[col].Length - 2);
    • 我做了一些小改动,这可能会奏效。如果不在您的调试环境中,就很难知道为什么该行是按照最初的方式编写的。
    • 现在文本文件中的所有内容都将被删除
    • 现在文本文件中的所有内容都被删除了..
    【解决方案2】:

    当我为我的搜索行删除创建两个不同的功能时,它起作用了..但我认为这是太长的代码..如果任何人有组合这个的想法..请发布答案..我会接受它!

    功能:在我的文本文件的第一列中进行关键字搜索

     public void do_name1()
        {
            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 (StreamReader sr = File.OpenText(textBox1.Text))
            {
                while ((old = sr.ReadLine()) != null)
                {
                    var st = old.Trim().Split(new char[] { '\t' });
                    if (st.Length > 1)
                    {
                        var tempCode1 = st[0].Substring(1, st[0].Length - 2);
                        if (!deleteCodeList.Contains(tempCode1)) 
                        {
                            sb.AppendLine(old);
                        }
                    }
                    else if (st.Length == 1)
                    {
                        //old = "\n";
                        sb.AppendLine(old);
    
                    }
                }
            }
            File.WriteAllText(textBox1.Text, sb.ToString());
        }
    

    功能:从第二列删除搜索到的文字。

    public void do_name1()
        {
            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 (StreamReader sr = File.OpenText(textBox1.Text))
            {
                while ((old = sr.ReadLine()) != null)
                {
                    var st = old.Trim().Split(new char[] { '\t' });
                    if (st.Length > 1)
                    {
                        var tempCode = st[1].Substring(1, st[1].Length - 2);
                        if (!deleteCodeList.Contains(tempCode)) 
                        {
                            sb.AppendLine(old);
                        }
                    }
                    else if (st.Length == 1)
                    {
                        //old = "\n";
                        sb.AppendLine(old);
    
                    }
                }
            }
            File.WriteAllText(textBox1.Text, sb.ToString());
        }
    

    【讨论】:

    • @MikeH 请回答...但我认为这太长了...
    • @yackov mandes zaig 你好……我已经在这里发布了答案
    【解决方案3】:

    您使用的是 st[1] 而不是 st[0].. st 是这一行中拆分函数的结果数组:

      var st = old.Trim().Split(new char[] { '\t' });
    

    因为数组索引是从零开始的,所以你应该使用 0 而不是 1

    编辑 - 基于 MikeH 的回答:

                var st = old.Trim().Split(new char[] { '\t' });
                bool hasDeleteMatch = false;
                //loop through all the columns starting at 0 and up to 1 (inclusive)
    
                var columns = new int[0, 2]; // the columns you want to remove
    
                for (int i = 0; i < columns.Length; i++) 
                {
                    int col = columns[i];
    
                    if (st[col].Length>2)
                    {
                      var tempCode = st[col].Substring(1, st[col].Length - 2);
                      if (!deleteCodeList.Contains(tempCode))
                      {
                          hasDeleteMatch = true; //we found a match, don't append to the new file
                          break;
                      }
                    }
                }
                if (!hasDeleteMatch) sb.AppendLine(old);
    

    【讨论】:

    • 我还需要删除第 2 列.. 现在我的第 1 列被删除了!!!
    猜你喜欢
    • 2015-12-21
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 2015-10-31
    • 2018-01-21
    • 2017-01-28
    • 1970-01-01
    • 2017-09-09
    相关资源
    最近更新 更多