Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文本文件中删除停止字_C#_Regex_C# 4.0 - Fatal编程技术网

C# 从文本文件中删除停止字

C# 从文本文件中删除停止字,c#,regex,c#-4.0,C#,Regex,C# 4.0,我想从文本文件中删除停止字,为此我编写了以下代码 TextWriter tw = new StreamWriter("D:\\output.txt"); private void button1_Click(object sender, EventArgs e) { StreamReader reader = new StreamReader("D:\\input1.txt"); string line;

我想从文本文件中删除停止字,为此我编写了以下代码

 TextWriter tw = new StreamWriter("D:\\output.txt");
 private void button1_Click(object sender, EventArgs e)
        {
            StreamReader reader = new StreamReader("D:\\input1.txt");
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                string[] parts = line.Split(' ');
                string[] stopWord = new string[] { "is", "are", "am","could","will" };
                foreach (string word in stopWord)
                {
                    line = line.Replace(word, "");
                    tw.Write("+"+line);
                }
                tw.Write("\r\n");
            } 

但是它不会在输出文件中显示结果,并且输出文件仍然为空。

尝试在
中使用(){}
子句包装
StreamWriter
StreamReader

using (TextWriter tw = new StreamWriter(@"D:\output.txt")
{
  ...
}

您可能还想在最后调用
tw.Flush()

以下操作对我来说是正常的。然而,这不是一个好方法,因为它将删除停止词,即使它们是一个较大单词的一部分。此外,它也不会清除删除的单词之间的额外空格

string[] stopWord = new string[] { "is", "are", "am","could","will" };

TextWriter writer = new StreamWriter("C:\\output.txt");
StreamReader reader = new StreamReader("C:\\input.txt");

string line;
while ((line = reader.ReadLine()) != null)
{
    foreach (string word in stopWord)
    {
        line = line.Replace(word, "");
    }
    writer.WriteLine(line);
}
reader.Close();
writer.Close();

另外,我建议在创建流时使用
using
语句,以确保文件及时关闭。

您应该在using语句中包装IO对象,以便正确处理它们

using (TextWriter tw = new TextWrite("D:\\output.txt"))
{
    using (StreamReader reader = new StreamReader("D:\\input1.txt"))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            string[] parts = line.Split(' ');
            string[] stopWord = new string[] { "is", "are", "am","could","will" };
            foreach (string word in stopWord)
            {
                line = line.Replace(word, "");
                tw.Write("+"+line);
            }
        }
    }
}

正则表达式可能非常适合该作业:

        Regex replacer = new Regex("\b(?:is|are|am|could|will)\b");
        using (TextWriter writer = new StreamWriter("C:\\output.txt"))
        {
            using (StreamReader reader = new StreamReader("C:\\input.txt"))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    replacer.Replace(line, "");
                    writer.WriteLine(line);
                }
            }
            writer.Flush();
        }
此方法仅将单词替换为空格,如果停止单词是另一个单词的一部分,则不处理它们


祝你的任务好运。

检查:-你正在关闭输出文件StreamWriter吗?你真的需要表达你的问题。。。以问题的形式。@jonathan:先生,这段代码工作不正常,我想从文本文件中删除stop word这是stackoverflow,用于询问技术问题。我发布的代码修复了代码中的错误。为了给您提供额外的帮助,我还解释了您正在采取的方法的一些问题。如果您有其他问题,您可能想发布另一个问题。但我强烈建议你学着说得比“工作不正常”更具体,因为“工作不正常”绝对不能说明你的问题。