不删除"0"的行

-1 c# string text text-files

我在我的C#应用​​程序中编写了一个函数,它应该在"0"到来时删除整行.但我不知道我的输出文本文件没有删除它.

我怎样才能解决这个问题.

代码段:

   public void do_name()
    {
        string[] search_text = new string[] { "PCB", "DOC", "PCB1", "DOC1" };
        string old;
        StringBuilder sb = new StringBuilder();
        using (StreamReader sr = File.OpenText(textBox1.Text))
        {
            while ((old = sr.ReadLine()) != null)
            {
                if (old.Contains(search_text[0]) || old.Contains(search_text[1]) ||
                    old.Contains(search_text[2]) || old.Contains(search_text[3]) ||
                   old.Split(" ".ToArray()).Equals("0"))
                     //here delete's the line where, "PCB", "DOC", "PCB1", "DOC1" is coming.
                     //but not '0"
                    continue;
                else
                    sb.AppendLine(old);
            }
            sr.Close();
        }
        File.WriteAllText(textBox1.Text, sb.ToString());
    }
Run Code Online (Sandbox Code Playgroud)

我的输入文本文件如下所示:

       "CN5"    "210-0141"  "PHOENIX/8/150/V/F" "353.441"   "115.951"
       "CN8"    "210-0141"  "PHOENIX/8/150/V/F" "317.881"   "115.824"
       "CN9"    "210-0141"  "PHOENIX/8/150/V/F" "265.176"   "115.951"
        "*1"    "210-0150"  ""  "0" "0"
        "*10"   "210-0150"  ""  "0" "0"
        "*11"   "210-0150"  ""  "0" "0"
        "*12"   "210-0150"  ""  "0" "0"
        "*13"   "210-0150"  ""  "0" "0"
        "*14"   "210-0150"  ""  "0" "0"
        "*15"   "210-0150"  ""  "0" "0"
Run Code Online (Sandbox Code Playgroud)

D S*_*ley 5

old.Split(" ".ToArray()).Equals("0"))
Run Code Online (Sandbox Code Playgroud)

始终是假的,因为Split返回数组和你它compoaring一个字符串.

我怀疑你想要

old == "0"
Run Code Online (Sandbox Code Playgroud)

代替.要么你需要清楚你的条件(字符串中的0 任何0一个?A作为分割值之一??作为第一个分割值的"0" ?)

作为分裂值之一

然后用

old.Split('\t').Contains(@"""0"""))
Run Code Online (Sandbox Code Playgroud)

请注意,Split字符数组的重载使用params关键字,因此您可以传入单个字符而不是将字符串转换为字符数组.我还包括0引号,因为这是您的输入数据的格式.

  • 或者`old.StartsWith("0")`或`old.Contains("0")`,它几乎取决于"0即将到来"的含义. (3认同)