[英]VB.NET Array Contains method does not work
In VB.NET I am trying to determine in a given string exists in a String Array. 在VB.NET中,我试图确定一个字符串数组中存在的给定字符串。 According to my research the Array has a 'Contains' method that I can use, so the Code looks something like this:
根据我的研究,Array有一个我可以使用的'Contains'方法,所以Code看起来像这样:
Dim fileTypesZ As String() = {"PDF", "TXT", "DOC", "DOCX", "XLS", "XLSX", "JPG", "JPGE", "BMP", "GIF"}
If (fileTypesZ.Contains(tempTest)) Then
End If
However, VB.NET is saying 'Contains' is not a member of 'System.Array'. 但是,VB.NET说'Contains'不是'System.Array'的成员。 Is there another method that I can use?
还有其他方法可供我使用吗?
There is no Contains
on Array
, but there is Enumerable.Contains
, which is an extension method that works on arrays. Array
上没有Contains
,但有Enumerable.Contains
,这是一种适用于数组的扩展方法。
Make sure to include Imports System.Linq
at the top of your file, and that you're referencing System.Core.dll
in your project references. 确保在文件顶部包含
Imports System.Linq
,并在项目引用中引用System.Core.dll
。
What framework are you working with? 你在用什么框架? I ran this in 4 Full and it worked:
我在4 Full中运行它并且它有效:
Sub Main()
Dim fileTypesZ As String() = {"PDF", "TXT", "DOC", "DOCX", "XLS", "XLSX", "JPG", "JPGE", "BMP", "GIF"}
If (fileTypesZ.Contains("PDF")) Then
MsgBox("Yay")
End If
End Sub
Keep in mind array.contains uses equality, so "PDF" works, "PD" does not. 请记住,array.contains使用相等,因此“PDF”起作用,“PD”不起作用。 You may need to iterate with indexof if you are looking for partial matches.
如果要查找部分匹配,则可能需要使用indexof进行迭代。
In that case try: Dim fileTypesZ As String() = {"PDF", "TXT", "DOC", "DOCX", "XLS", "XLSX", "JPG", "JPGE", "BMP", "GIF"} 在这种情况下,尝试:Dim fileTypesZ As String()= {“PDF”,“TXT”,“DOC”,“DOCX”,“XLS”,“XLSX”,“JPG”,“JPGE”,“BMP”,“ GIF“}
If (fileTypesZ.Contains("PD")) Then
MsgBox("Yay")
Else
For i = 0 To fileTypesZ.Length - 1
If fileTypesZ(i).IndexOf("PD") = 0 Then
MsgBox("Yay")
End If
Next
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.