Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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/1/vb.net/15.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
String 从列表中删除所有出现的单词_String_Vb.net_List_Removeall - Fatal编程技术网

String 从列表中删除所有出现的单词

String 从列表中删除所有出现的单词,string,vb.net,list,removeall,String,Vb.net,List,Removeall,给出了单词列表 Word Word Base Ball Ball Apple Cardinal 我如何删除所有出现的“单词”和“球”?我做了一些研究,发现了list.RemoveAll函数。我只是对如何在字符串列表上实现这一点有点困惑。对于这样的内容,将列表转换为数组会更好吗 Public Function getListOfWords() As String() listOfWords.RemoveAll("the") Dim arrayA() As String = lis

给出了单词列表

Word Word Base Ball Ball Apple Cardinal
我如何删除所有出现的“单词”和“球”?我做了一些研究,发现了
list.RemoveAll
函数。我只是对如何在字符串列表上实现这一点有点困惑。对于这样的内容,将列表转换为数组会更好吗

Public Function getListOfWords() As String()
    listOfWords.RemoveAll("the")
    Dim arrayA() As String = listOfWords.ToArray
    Return arrayA
End Function

这就是如何在列表(字符串)上使用RemoveAll


在此上下文中,需要一个函数来接收字符串并返回布尔值()。函数通过重复调用一个接一个地接收序列中包含的元素,并应返回true以删除元素,或返回false以保留元素

这就是如何在列表(字符串)上使用RemoveAll


在此上下文中,需要一个函数来接收字符串并返回布尔值()。该函数通过重复调用一个接一个地接收序列中包含的元素,并应返回true以删除该元素,或返回false以保留该元素

我知道它已被应答和接受。但是bool谓词似乎有点硬编码,在可伸缩性方面,也就是说,如果要删除的单词是“Word”、“Ball”和“Apple”,该怎么办?现在必须在代码中更改谓词。我想您可以在谓词中聚合代码,但现在事情变得很愚蠢

您还可能认识到该问题需要一个解决方案,它将接受排除列表中任意数量的元素。这是一个你可以使用的函数

Public Function getListOfWords(words As IEnumerable(Of String),
                               wordsToRemove As IEnumerable(Of String)) As String()

    Dim wordsRemoved = From word In words
                       Group Join wordToRemove In wordsToRemove
                           On word Equals wordToRemove Into Group
                       From g In Group.DefaultIfEmpty
                       Where g = ""
                       Select word

    Return wordsRemoved.ToArray()
End Function
现在,您可以在任意数量的列表中包含这两个集合并调用函数,而不是两个bool谓词

Dim words = {"Word", "Word", "Base", "Ball", "Ball", "Apple", "Cardinal"}
Dim wordsToRemove = {"Word", "Ball"}
Dim wordsRemoved = getListOfWords(words, wordsToRemove)
For Each s In wordsRemoved
    Console.WriteLine(s)
Next

我知道它已经被回答和接受了。但是bool谓词似乎有点硬编码,在可伸缩性方面,也就是说,如果要删除的单词是“Word”、“Ball”和“Apple”,该怎么办?现在必须在代码中更改谓词。我想您可以在谓词中聚合代码,但现在事情变得很愚蠢

您还可能认识到该问题需要一个解决方案,它将接受排除列表中任意数量的元素。这是一个你可以使用的函数

Public Function getListOfWords(words As IEnumerable(Of String),
                               wordsToRemove As IEnumerable(Of String)) As String()

    Dim wordsRemoved = From word In words
                       Group Join wordToRemove In wordsToRemove
                           On word Equals wordToRemove Into Group
                       From g In Group.DefaultIfEmpty
                       Where g = ""
                       Select word

    Return wordsRemoved.ToArray()
End Function
现在,您可以在任意数量的列表中包含这两个集合并调用函数,而不是两个bool谓词

Dim words = {"Word", "Word", "Base", "Ball", "Ball", "Apple", "Cardinal"}
Dim wordsToRemove = {"Word", "Ball"}
Dim wordsRemoved = getListOfWords(words, wordsToRemove)
For Each s In wordsRemoved
    Console.WriteLine(s)
Next

假设重复项一个接一个地出现:

Dim s = "Word Word Base Ball Ball Apple Cardinal"
Dim pattern = "(?i)(?'word'[a-z]+)(\s+\k'word')"
Dim s2 = Regex.Replace(s, pattern, "${word}")

假设重复项一个接一个地出现:

Dim s = "Word Word Base Ball Ball Apple Cardinal"
Dim pattern = "(?i)(?'word'[a-z]+)(\s+\k'word')"
Dim s2 = Regex.Replace(s, pattern, "${word}")

你自己有没有试过?你自己有没有试过?