Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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#_Arrays_Asp.net Mvc_Loops_Foreach - Fatal编程技术网

C# 循环遍历字符串并删除指定单词的任何匹配项

C# 循环遍历字符串并删除指定单词的任何匹配项,c#,arrays,asp.net-mvc,loops,foreach,C#,Arrays,Asp.net Mvc,Loops,Foreach,我试图从任何字符串数组中删除所有连词和代词(称为数组A),要删除的单词从文本文件中读取并转换为字符串数组(称为数组B) 我需要的是获取数组A的第一个元素,并将其与数组B中的每个单词进行比较,如果单词匹配,我想删除数组A中的单词 例如: 数组A=[0]我[1]希望[2]去[3]回家[5]和[6]睡觉 数组B=[0]I[1]和[2]转到[3]到 结果=数组A=[0]需要[1]主[2]睡眠 //remove any duplicates,conjunctions and Pronouns

我试图从任何字符串数组中删除所有连词和代词(称为数组A),要删除的单词从文本文件中读取并转换为字符串数组(称为数组B)

我需要的是获取数组A的第一个元素,并将其与数组B中的每个单词进行比较,如果单词匹配,我想删除数组A中的单词

例如:

数组A=[0]我[1]希望[2]去[3]回家[5]和[6]睡觉
数组B=[0]I[1]和[2]转到[3]到

结果=数组A=[0]需要[1]主[2]睡眠

//remove any duplicates,conjunctions and Pronouns
        public IQueryable<All_Articles> removeConjunctionsProNouns(IQueryable<All_Articles> myArticles)
        {
            //get words to be removed
            string text = System.IO.File.ReadAllText("A:\\EnterpriceAssigment\\EnterpriceAssigment\\TextFiles\\conjunctions&ProNouns.txt").ToLower();
            //split word into array of strings 
            string[] wordsToBeRemoved = text.Split(',');
            //all articles
            foreach (var article in myArticles)
            {
               //split articles into words
                string[] articleSplit = article.ArticleContent.ToLower().Split(' ');
                //loop through array of articles words
                foreach (var y in articleSplit)
                {
                    //loop through words to be removed from articleSplit
                    foreach (var x in wordsToBeRemoved)
                    {
                        //if word of articles matches word to be removed, remove word from article
                        if (y == x)
                        {
                            //get index of element in array to be removed
                            int g = Array.IndexOf(articleSplit,y);
                            //assign elemnt to ""
                            articleSplit[g] = "";
                        }
                    }
                }
                //re-assign splitted article to string
                article.ArticleContent = articleSplit.ToString();
            }
            return myArticles;
        }
//删除任何重复项、连词和代词
公共IQueryable移除连接代词(IQueryable myArticles)
{
//获取要删除的单词
string text=System.IO.File.ReadAllText(“A:\\enterpricassigment\\enterpricassigment\\TextFiles\\conjunctions&denos.txt”).ToLower();
//将单词拆分为字符串数组
string[]wordsToBeRemoved=text.Split(',');
//所有文章
foreach(myArticles中的var文章)
{
//分词
字符串[]articleSplit=article.ArticleContent.ToLower().Split(“”);
//循环遍历文章和单词的数组
foreach(articleSplit中的变量y)
{
//循环遍历要从articleSplit中删除的单词
foreach(var x,以字表示,已移动)
{
//若文章中的单词与要删除的单词匹配,则从文章中删除单词
如果(y==x)
{
//获取要删除的数组中元素的索引
int g=Array.IndexOf(articleSplit,y);
//将元素分配给“”
articleSplit[g]=“”;
}
}
}
//将拆分的项目重新分配给字符串
article.ArticleContent=articleSplit.ToString();
}
归还我的物品;
}

如果可能的话,我需要数组A没有重复/不同的值

您想删除停止词。您可以在Linq的帮助下完成:


您想删除停止字。您可以在Linq的帮助下完成:


您可能已经在代码中找到了答案。我相信你的代码可以被清理一下,就像我们所有的代码一样。你循环阅读articleSplit并把每个单词都抽出来。然后将该单词与循环中wordsToBeRemoved数组中的单词逐一进行比较。使用条件进行比较,如果为true,则从原始数组中删除项,或者至少尝试

我将创建另一个结果数组,然后显示、使用或使用该数组减去要排除的单词后的任何内容。 循环通过articleSplit 弧分裂中的foreach x 用语言表达的每一句话都令人感动 如果x!=y newArray.Add(x)

然而,这是相当多的工作。您可能需要使用array.filter,然后以这种方式添加。有一百种方法可以实现这一点

以下是一些有用的文章:
这些将使你免于所有的循环

您的代码中可能已经有了答案。我相信你的代码可以被清理一下,就像我们所有的代码一样。你循环阅读articleSplit并把每个单词都抽出来。然后将该单词与循环中wordsToBeRemoved数组中的单词逐一进行比较。使用条件进行比较,如果为true,则从原始数组中删除项,或者至少尝试

我将创建另一个结果数组,然后显示、使用或使用该数组减去要排除的单词后的任何内容。 循环通过articleSplit 弧分裂中的foreach x 用语言表达的每一句话都令人感动 如果x!=y newArray.Add(x)

然而,这是相当多的工作。您可能需要使用array.filter,然后以这种方式添加。有一百种方法可以实现这一点

以下是一些有用的文章:
这些将使你免于所有的循环

您正在查找IEnumerable。除非传递的参数应用于输入序列,并且参数列表中不存在的输入序列的每个元素只返回一次

比如说

string inputText = "I want this string to be returned without some words , but words should have only one occurence";
string[] excludedWords = new string[] {"I","to","be", "some", "but", "should", "have", "one", ","};

var splitted = inputText.Split(' ');
var result = splitted.Except(excludedWords);
foreach(string s in result)
    Console.WriteLine(s);

// ---- Output ----
want
this
string
returned
without
words   <<-- This appears only once
only
occurence

您正在查找IEnumerable。除了,传递的参数应用于输入序列,并且参数列表中不存在的输入序列的每个元素只返回一次

比如说

string inputText = "I want this string to be returned without some words , but words should have only one occurence";
string[] excludedWords = new string[] {"I","to","be", "some", "but", "should", "have", "one", ","};

var splitted = inputText.Split(' ');
var result = splitted.Except(excludedWords);
foreach(string s in result)
    Console.WriteLine(s);

// ---- Output ----
want
this
string
returned
without
words   <<-- This appears only once
only
occurence

你是说这个吗?当同一个单词的出现次数超过1次时,会删除所有出现次数还是仅删除第一次出现次数?我正在处理字符串数组而不是IEnumerablearray是IEnumerable。除非结果是一组不同的源值不在异常列表中,您的意思是?当同一个单词的出现次数超过1次时,会删除所有出现次数还是仅删除第一次出现次数?我正在处理字符串数组而不是IEnumerablearray是IEnumerable。exception结果是不在异常列表中的一组不同的源值
string inputText = "I want this string to be returned without some words , but words should have only one occurence";
string[] excludedWords = new string[] {"I","to","be", "some", "but", "should", "have", "one", ","};

var splitted = inputText.Split(' ');
var result = splitted.Except(excludedWords);
foreach(string s in result)
    Console.WriteLine(s);

// ---- Output ----
want
this
string
returned
without
words   <<-- This appears only once
only
occurence
string text = File.ReadAllText(......).ToLower();
string[] wordsToBeRemoved = text.Split(',');
foreach (var article in myArticles)
{
    string[] articleSplit = article.ArticleContent.ToLower().Split(' ');
    var result = articleSplit.Except(wordsToBeRemoved);
    article.ArticleContent = string.Join(" ", result);
}