簡體   English   中英

vb如何在字符串中查找字符串並將字符串返回到變量中

[英]vb how to find a string in a string and return the string into a variable

我試圖從字典中讀取一個字符串,並與我的密碼進行比較,並采取已建立的工作,並將其放入變量。

我已經嘗試過使用instr但它只返回位置而不是值。

Dim mypos As Integer
     For i = 0 To UBound(dictionary_english, 1)
       mypos = 1
       mypos = InStr(password, dictionary_english(i))   
       If mypos > 0 Then Exit For Else
     Next

我正在努力實現以下目標。 mainstr =“sefsfdogsdfr”searchstr =“dog”搜索mainstr for dog並將dog返回變量。

此代碼使用.net提供的舊VB方法的一些替換。 如果您使用.net方法使用相同的波長,那么查看C#代碼會更容易,並且您將查看C#代碼,因為大多數示例都是用c#編寫的。

Private dictionary_english() As String = {"sam", "PeterdogRabbit", "George"}
Private Function ExistsInDictionary(StringToSearch As String) As Boolean
    Dim result As Boolean
    For index = 0 To dictionary_english.GetUpperBound(0)
        If dictionary_english(index).Contains(StringToSearch) Then
            Return True
        End If
    Next
    Return result
End Function
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim searchTerm = "dog"
    Dim IsInDictionary = ExistsInDictionary(searchTerm)
    MessageBox.Show(IsInDictionary.ToString)
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM