如何从我的文本文件中删除两个字符串,然后启动我的下面的代码

时间:2014-11-03 16:23:54

标签: c# wpf string visual-studio-2010 visual-studio

我想在阅读时始终从文本文件中删除两个字符串。 DOC PCB 。所以,当这两个字符串即将到来时,我想删除它们并开始我的过程。请查看我的输出文件https://imageshack.com/i/ipUcSIo2j 我需要输出文件没有这些字符串

我该怎么办?但我需要输出位置列编号不应该呕吐之间的任何数字

我的代码段:

         string search_text = "PCB";
        string old;
        string n = "";
        StreamReader sr = File.OpenText(textBox1.Text);
        while ((old = sr.ReadLine()) != null)
        {
            if (!old.Contains(search_text))
            {
                n += old + Environment.NewLine;
            }
        }
        sr.Close();
        File.WriteAllText(textBox1.Text, n);

4 个答案:

答案 0 :(得分:1)

首先,请看这个问题并接受答案Efficient way to delete a line from a text file

所以你需要这样做,阅读文件,将你想要的行写入新文件,并跳过你不想要的行。实际上有 NO 方法从文本文件中删除行,这就是你伪造删除的方法。

然后在新文件上运行您的代码。

您是否需要帮助确定要删除的行,或者您对此有用吗?

答案 1 :(得分:1)

我认为这应该很简单。

string[] search = new string[] {"DOC","PCB"};
var lines = System.IO.File.ReadAllLines(@"D:\MyFile.txt");
var result1 = lines.Where(item => !item.Contains(search[0]) && !item.Contains(search[1]));
StringBuilder sb = new StringBuilder();
foreach (string line in result1)
{
    sb.AppendLine(line);
}

答案 2 :(得分:0)

我得到了答案。为了删除两个字加上整行,我使用了两个不同的功能,其中每个功能服务于不同的角色。请看看我的答案。我知道这有点长,可以用阵列系统来完成。如果有任何更好的答案,我准备接受它。

 public void do_name()
    {
        string search_text = "PCB";
        string old;
        string n = "";
        StreamReader sr = File.OpenText(textBox1.Text);
        while ((old = sr.ReadLine()) != null)
        {
            if (!old.Contains(search_text))// || !old.Contains(next_text)) 
            {
                n += old + Environment.NewLine;
            }
        }
        sr.Close();
        File.WriteAllText(textBox1.Text, n);
    }

    public void do_name1()
    {
        string next_text = "DOC";
        string old;
        string n = "";
        StreamReader sr = File.OpenText(textBox1.Text);
        while ((old = sr.ReadLine()) != null)
        {
            if (!old.Contains(next_text))// || !old.Contains(next_text)) 
            {
                n += old + Environment.NewLine;
            }
        }
        sr.Close();
        File.WriteAllText(textBox1.Text, n);
    }
    Dictionary<string, int> namesForCarousels = new Dictionary<string, int>();
    private void button1_Click(object sender, EventArgs e)
    {
        string path = "";
        string filename_noext = "";
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.InitialDirectory = @"C:\files";
        ofd.Filter = "TXT files (*.txt)|*.txt";
        ofd.FilterIndex = 2;
        ofd.RestoreDirectory = true;

        if (ofd.ShowDialog() == DialogResult.OK)
        {
            filename_noext = System.IO.Path.GetFileName(ofd.FileName);
            path = Path.GetFullPath(ofd.FileName);
            textBox1.Text = path;
            do_name();
            do_name1();
            //MessageBox.Show(filename_noext, "Filename"); - - -> switching.grf
            //MessageBox.Show(path, "path");
            //Give the column width according to column index.
            int[] cols = new int[] { 15, 15, 25, 15, 15 };
            string[] strLines = System.IO.File.ReadAllLines(textBox1.Text);

            StringBuilder sb = new StringBuilder();
            string line = string.Empty;
            string LastComment = string.Empty;
            string CarouselName = "Carousel";
            int iCarousel = 0;

            for (int i = 0; i < strLines.Length; i++)
            {

                line = RemoveWhiteSpace(strLines[i]).Trim();
                string[] cells = line.Replace("\"", "").Split('\t');

                for (int c = 0; c < cells.Length; c++)
                    sb.Append(cells[c].Replace(" ", "_").PadRight(cols[c]));

                if (cells.Length > 1)
                {
                    var name = cells[1];
                    int carouselNumber;
                    if (namesForCarousels.TryGetValue(name, out carouselNumber) == false)
                    {
                        carouselNumber = iCarousel++;
                        namesForCarousels[name] = carouselNumber;
                    }
                    if (i == 0)
                        sb.Append("Location".PadRight(15));
                    else
                        sb.Append(String.Format("{0}:{1}", CarouselName, carouselNumber).PadRight(15));
                }
                sb.Append("\r\n");
            }

            System.IO.File.WriteAllText(textBox1.Text, sb.ToString());
           // do_name1();
            Application.Exit();
        }
    }

答案 3 :(得分:-1)

将该代码放在InitializeComponent();

下的ctor / constructor中