Getting multiple index values ​​from an array

I created an array of strings from AZ that will contain indices from 0-25.

Then I have a text field, and when I enter text into a text field, how can I get the index number of the array associated with the entered text?

For example, I enter "AB" in the text box, and the returned index should be 0 and 1.

The code below can only return an index if I enter only one alphabetic alphabet. How to return index number for many alphabets?

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
+3
source share
3 answers

Compiled and works fine:

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

It will appear in the message box, respectively:

'l   e  n   i  e  l
 11, 4, 13, 8, 4, 11
+1
source

, , .split . , .

0

.

:

1)

Dim theText = TextBox2.Text

2) For each letter in this text, get a number (and display it as a message

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

When compiling, it should pop up each number, one by one. I used the same function that you used to translate the character (i.e. Get the result). There are other ways to do this, I just wanted to consider your main question, indicating that you can iterate over all the characters in a string.

I hope this helps,

- gMale

0
source

Source: https://habr.com/ru/post/1753970/


All Articles