2010-07-09 65 views
3

我從A-Z創建了一個字符串數組,它將包含0-25的索引。從數組中獲取多個索引值

然後我有一個文本框,當我在文本框中輸入文本時,如何獲得與我輸入的文本相關聯的數組的索引號?

例如,我輸入「AB」到文本框,和指數收益率應該在0和1

下面的代碼是唯一能,如果我只輸入一個字母表返回索引。如何返回許多字母的索引號?

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click 

    Dim abc(25) As String 
    abc(0) = "a" 
    abc(1) = "b" 
    abc(2) = "c" 
    abc(3) = "d" 
    abc(4) = "e" 
    abc(5) = "f" 
    abc(6) = "g" 
    abc(7) = "h" 
    abc(8) = "i" 
    abc(9) = "j" 
    abc(10) = "k" 
    abc(11) = "l" 
    abc(12) = "m" 
    abc(13) = "n" 
    abc(14) = "o" 
    abc(15) = "p" 
    abc(16) = "q" 
    abc(17) = "r" 
    abc(18) = "s" 
    abc(19) = "t" 
    abc(20) = "u" 
    abc(21) = "v" 
    abc(22) = "w" 
    abc(23) = "x" 
    abc(24) = "y" 
    abc(25) = "z" 

    Dim result = abc.Where(Function(a) a.Contains(TextBox2.Text)).Select(Function(s) Array.IndexOf(abc, s)).ToArray() 

    Dim x As Integer 
    For Each x In result 
     MsgBox(x) 
    Next 

End Sub 

回答

0

首先想到的是在文本框輸入中使用.split方法來獲取字符數組。對數組中的每個元素執行搜索,將每個匹配添加到結果數組中。

0

有一百萬種方法可以解決這個問題。

一種解決方案:

1)獲取文本

Dim theText = TextBox2.Text 

2)爲在該文本中的每個字母,得到的數量(並顯示它作爲提供的消息

Dim c As Char 
For Each c In theText 
    MsgBox(translateCharacter(c)) 
Next 

Public Function translateCharacter(ByVal c As Char) As Integer 
    translateCharacter = abc.Where(Function(a) a.Contains(c)).Select(Function(s) Array.IndexOf(abc, s)).ToArray() 
End Function 

編譯時,它應該彈出每個數字,一個接一個地使用你用來翻譯字符的相同函數(即得到結果)還有其他方法可以做到這一點,我只是想解決你的主要問題問題是指出你可以迭代字符串中的所有字符。

我希望幫助,

--gMale

1

編譯和運行完美:

Module Module1 

    Sub Main() 

     Test("Leniel") 

    End Sub 
    Sub Test(ByVal text As String) 

     Dim alphabet() As String = {"a", "b", "c", "d", "e", 
            "f", "g", "h", "i", "j", 
            "k", "l", "m", "n", "o", 
            "p", "q", "r", "s", "t", 
            "u", "v", "w", "x", "y", "z"} 

     Dim indexes = From letter In text.ToCharArray() _ 
      Select Array.IndexOf(alphabet, letter.ToString().ToLower()) 

     Dim i As Integer 
     For Each i In indexes 
      MsgBox(i) 
     Next 

    End Sub 

End Module 

它會在消息框中分別顯示:

'l e n i e l 
11, 4, 13, 8, 4, 11