且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除除特定字符串外的所有文本

更新时间:2023-02-21 13:19:08

只需逐行读取文件,并忽略所有不以单词"clear"开头或第一个字符为'a'的文件.双引号.将所有其他行写到新文件中.
Just read the file line by line and ignore any that do not begin with the word "clear", or whose first character is a double quote. Write out all other lines to a new file.


因此,您需要首先创建包含大文本的文件"mytext.txt",然后创建文件"myresult.txt" !这两个文件必须在可执行文件附近...
之后,使用以下代码创建项目:

So you need to create first the file "mytext.txt" that contains the big text, and then you create the file "myresult.txt"! The two files must be near the executable file...
After that you create the project with the folowing code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MyCodeProjectAnswer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string line = "";
            string[] substr = new string[1000];
            StreamReader reader = new StreamReader("mytext.txt");
            StreamWriter writer = new StreamWriter("myresult.txt",true);

            while (!reader.EndOfStream)
            {
                line = reader.ReadLine();
                substr = line.Split(' ');

                if (substr[0] == "clear")
                    writer.WriteLine(line);
            }
            reader.Close();
            writer.Close();
        }
    }
}



最后运行它,所需的输出在"myresult.txt"文件中.

问候,
KZ



Finally run it, and the output you want is in the "myresult.txt" file.

Regards,
KZ