[英]VB.NET Array Contains method does not work
在VB.NET中,我試圖確定一個字符串數組中存在的給定字符串。 根據我的研究,Array有一個我可以使用的'Contains'方法,所以Code看起來像這樣:
Dim fileTypesZ As String() = {"PDF", "TXT", "DOC", "DOCX", "XLS", "XLSX", "JPG", "JPGE", "BMP", "GIF"}
If (fileTypesZ.Contains(tempTest)) Then
End If
但是,VB.NET說'Contains'不是'System.Array'的成員。 還有其他方法可供我使用嗎?
Array
上沒有Contains
,但有Enumerable.Contains
,這是一種適用於數組的擴展方法。
確保在文件頂部包含Imports System.Linq
,並在項目引用中引用System.Core.dll
。
你在用什么框架? 我在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
請記住,array.contains使用相等,因此“PDF”起作用,“PD”不起作用。 如果要查找部分匹配,則可能需要使用indexof進行迭代。
在這種情況下,嘗試: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.