检查一个字符串是否包含所有其他字符串 [英] Check if a string contains all other strings
问题描述
我正在尝试编写软件的一部分,以尝试显示与搜索条件匹配的结果。
I am trying to code a part of a software where I try to show the results that match search criteria.
我有一个文本框,可以在其中键入一个或我想搜索更多的单词,并包含4个不同的列和12行的listview。这个想法是每个listview行包含很多单词,我只想查看包含我在文本框中键入的所有单词的行。我已经完成了仅搜索一个术语的代码。我遇到的问题是我不完全了解该怎么做,而是使用多个术语而不是仅一个术语。
I have a textbox where I can type one or more words I want to search and a listview that contains 4 different columns and a dozen rows. The idea is that each listview row contains lots of words and I want to see only the rows that contain all the words I have typed in the textbox. I have finished the code that searches for one term only. The problem I am having is that I don't fully understand how to do the same, but using multiple terms instead of one term only.
在文本框中,我写了我要搜索的单词用空格隔开。我有一个变量,其中列表视图行的整个内容由:分隔(示例=> col1row1content:col1row2content:col1row3content等)。总结一下,我想检查一个字符串(一行的全部内容)是否包含所有其他字符串(我在文本框中键入的每个单词)。
In the textbox, I write the words I want to search separated by a space. I have a variable where I keep the whole content of the listview row separated by : (example => col1row1content:col1row2content:col1row3content,etc). Summarizing, I want to check if a string (the full content of a row) contains all other strings (each word I have typped in the textbox).
这是我已实现的代码:
Dim textboxFullContentArray As String() = textboxSearch.Split(New Char() {" "c})
Dim Content As String
Dim containsAll As Boolean = False
Dim wholeRowContent(listviewMain.Items.Count - 1) As String ' each index of the array keeps the entire row content (one array contains all 4 cells of the row)
' wholeRowContent contains in one index the entire content of a row. That means,
' the index contains the 4 cells that represent an entire row.
' The format is like "rowData1:rowData2:rowData3:rowData4" (omitted for simplicity)
For Q As Integer = 0 To listviewMain.Items.Count - 1
For Each Content In textboxFullContentArray
If wholeRowContent(Q).ToLower.Contains(Content) Then
containsAll = True
' rest of the code...
ElseIf Not wholeRowContent(Q).ToLower.Contains(Content) Then
containsAll = False
Exit For
End If
Next
Next
但是,当然,此代码显示了误报,我认为这不是一个好的解决方案。我认为它一定要容易得多,并且使这个概念过于复杂。
But of course, this code is showing false positives and I think it's not a good solution. I think it must be much easier and I am overcomplicating the concept.
我正在使用VB.Net 2013
I am using VB.Net 2013
推荐答案
您可以确定 String
是否包含用单行代码包含的所有子字符串列表:
You can determine whether a String
contains all of a list of substrings with a single line of code:
If substrings.All(Function(s) str.IndexOf(s, StringComparison.OrdinalIgnoreCase) >= 0) Then
请注意,我实际上已经实现了不区分大小写的比较,而不是使用 ToLower
或 ToUpper
。
Notice that I have actually implemented a case-insensitive comparison, rather than using ToLower
or ToUpper
.
调用 IndexOf $似乎不太巧妙。 c $ c>而不是
包含
,但是您猜怎么着:包含
实际上是调用 IndexOf
内部还是这样:
It may not seem as neat to call IndexOf
rather than Contains
but guess what: Contains
actually calls IndexOf
internally anyway:
public bool Contains(string value)
{
return this.IndexOf(value, StringComparison.Ordinal) >= 0;
}
如果需要不区分大小写的<$,则可以编写自己的扩展方法c $ c>包含方法:
You can write your own extension methods if you want a case-insensitive Contains
method:
<Extension>
Public Function Contains(source As String,
value As String,
comparisonType As StringComparison) As Boolean
Return source.IndexOf(value, comparisonType) >= 0
End Function
这篇关于检查一个字符串是否包含所有其他字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!