Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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#_Linq - Fatal编程技术网

C# 从字符串中删除单词列表

C# 从字符串中删除单词列表,c#,linq,C#,Linq,我有一个要从字符串中删除的单词列表,我使用以下方法 string stringToClean = "The.Flash.2014.S07E06.720p.WEB-DL.HEVC.x265.RMTeam"; string[] BAD_WORDS = { "720p", "web-dl", "hevc", "x265", "Rmteam", ".

我有一个要从字符串中删除的单词列表,我使用以下方法

string stringToClean = "The.Flash.2014.S07E06.720p.WEB-DL.HEVC.x265.RMTeam";

string[] BAD_WORDS = {
            "720p", "web-dl", "hevc", "x265", "Rmteam", "."
        };
    
var cleaned = string.Join(" ", stringToClean.Split(' ').Where(w => !BAD_WORDS.Contains(w, StringComparer.OrdinalIgnoreCase)));
但是它不工作,下面的文本被输出

.Flash.2014.S07E06.720p.WEB-DL.HEVC.x265.RMTeam


为此,最好创建一个可重用的方法,将字符串拆分为单词。我将作为string的扩展方法来执行此操作。如果您不熟悉扩展方法,请阅读

待办事项:

  • 如果文本以点“.ABC.DEF”开头,请确定要返回的内容
  • 如果文本以点结尾,请确定要返回的内容:“ABC.DEF”
  • 如果文本为空,请检查返回值是否为所需值

您的拆分/加入与您的输入不匹配

这就是说,这里有一个快速的一行:

string clean = BAD_WORDS.Aggregate(stringToClean, (acc, word) => acc.Replace(word, string.Empty));
这基本上是一种“减少”。虽然不是很好的表演,但是我们知道它是可以接受的。如果您必须使用一个非常大的字符串或大量的“单词”,您可以考虑另一个选项,但它应该适用于您给我们的示例案例


编辑:这种方法的缺点是你会得到部分。例如,在您的令牌数组中,您有“720p”,但我在这里建议的代码仍将与“720px”匹配,但仍有解决方法。例如,不使用
string
Replace
实现,您可以使用一个与分隔符匹配的正则表达式,比如
regex.Replace(acc,$“[.]{word}([.])”,“$1”)
(正则表达式未确认,但应该关闭,我为分隔符添加了一个捕获,以便将其放回下一个过程)

为什么输入字符串不包含空格却要用空格分隔?哦,我以前从某个地方复制了这段代码,如果单词之间有空格,它似乎可以工作,但是它在这种情况下不起作用我需要在任何情况下工作@gunr2171我投票结束这个问题,因为这是一个简单的打字错误,可以通过将
Split(“”)
替换为
Split(“”)
@gunr2171如何处理(.)和(空格)来解决?@hadikhodabandeh请对的不同重载进行一些研究以找出答案。
string text = "This is some wild text!"
List<string> words = text.ToWords().ToList();
var first3Words = text.ToWords().Take(3);
var lastWord = text.ToWords().LastOrDefault();
IEnumerable<string> badWords = ...
string inputText = ...
IEnumerable<string> validWords = inputText.ToWords().Except(badWords);
public static IEnumerable<string> ToWords(this string text)
{
    // find the next dot:
    const char dot = '.';
    int startIndex = 0;
    int dotIndex = text.IndexOf(dot, startIndex);
    while (dotIndex != -1)
    {
        // found a Dot, return the substring until the dot:
        int wordLength = dotIndex - startIndex;
        yield return text.Substring(startIndex, wordLength;

        // find the next dot      
        startIndex = dotIndex + 1;
        dotIndex = text.IndexOf(dot, startIndex);
    }

    // read until the end of the text. Return everything after the last dot:
    yield return text.SubString(startIndex, text.Length);
}
string clean = BAD_WORDS.Aggregate(stringToClean, (acc, word) => acc.Replace(word, string.Empty));