循环文本字符

时间:2014-03-03 08:23:27

标签: vb.net loops text

我正在尝试从文本框中捕获字符串,并根据该字符串中找到的每个字符运行条件。以下代码是我正在考虑的但它有效吗?

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim s As String = TextBox1.Text

        For Each c As Char In s
            If c = "A" Then
                MsgBox("Letter is A")
            ElseIf c = "B" Then
                MsgBox("Letter is A")
            ElseIf c = "C" Then
                MsgBox("Letter is C")

            End If
        Next
end sub

2 个答案:

答案 0 :(得分:0)

使用chararry分割它,你的文本框可能正在捕获一些unicode

Dim charArray() As Char = TextBox1.Text.ToCharArray

另请参阅msdn页面的精彩见解msdn

答案 1 :(得分:0)

试试这个:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim stringword() As Char
    stringword = TextBox1.Text.ToCharArray
    For i = 0 To stringword.Count - 1
        Select Case stringword(i)
            Case "a", "A"
                MsgBox("Letter is A")
            Case "B", "b"
                MsgBox("Letter is b")
            Case "C", "c"
                MsgBox("Letter is c")

        End Select
    Next
End Sub