VB中如何将String中的数字与字母提取出来? 用function来做,必须包含while。
Private Function changeToUp(ByVal inputdata) As String
        Dim input As String
        inputdata = txtTest.Text.Trim
        If inputdata.Length <= 0 Then                                     
            input = "Note :Please enter a string with letters or digits"
        Else
            'convert string's letters to upper case
            input = inputdata.ToUpper            Dim intLastPos = input.Length - 1
            Dim intPos As Integer = 0
            Do                Dim chrVar As Char = input.Chars(intPos)
                If chrVar >= "A" And chrVar <= "Z" Then
                    inputdata = input & chrVar
 ElseIf input(intPos).ToString.Equals("0") Then
                    inputdata = inputdata & "0"
                ElseIf input(intPos).ToString.Equals("1") Then
                    inputdata = inputdata & "1"
                ElseIf input(intPos).ToString.Equals("2") Then
                    inputdata = inputdata & "2"
                ElseIf input(intPos).ToString.Equals("3") Then
                    inputdata = inputdata & "3"
                ElseIf input(intPos).ToString.Equals("4") Then
                    inputdata = inputdata & "4"
                ElseIf input(intPos).ToString.Equals("5") Then
                    inputdata = inputdata & "5"
                ElseIf input(intPos).ToString.Equals("6") Then
                    inputdata = inputdata & "6"
                ElseIf input(intPos).ToString.Equals("7") Then
                    inputdata = inputdata & "7"
                ElseIf input(intPos).ToString.Equals("8") Then
                    inputdata = inputdata & "8"
                ElseIf input(intPos).ToString.Equals("9") Then
                    inputdata = inputdata & "9"                Else
                End If
                intPos += 1
            Loop Until intPos >= intLastPos
        End If
        Return inputdata
    End Function
这样的取值当输入标点符号的时候,产生问题。

解决方案 »

  1.   

    Public Class FormPalindromeRecognizer    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
            txtTest.Text = String.Empty
            lblAbout.Text = String.Empty
            txtTest.ReadOnly = False        btnClear.Enabled = False
            btnTest.Enabled = True        txtTest.Focus()
        End Sub
        Private Function changeToUp(ByVal inputdata) As String
            Dim input As String
            inputdata = txtTest.Text.Trim
            If inputdata.Length <= 0 Then                                      'Try statements
                input = "Note :Please enter a string with letters or digits"
            Else
                'convert string's letters to upper case
                input = inputdata.ToUpper            Dim intLastPos = input.Length - 1
                Dim intPos As Integer = 0
                Do                Dim chrVar As Char = input.Chars(intPos)
                    If chrVar >= "A" And chrVar <= "Z" Then
                        inputdata = input & chrVar                    'ElseIf Int(input(intPos)) >= 0 And Int(input(intPos)) <= 9 Then
                        '    input = input & Int(input(intPos))
                        'End If
                    ElseIf input(intPos).ToString.Equals("0") Then
                        inputdata = inputdata & "0"
                    ElseIf input(intPos).ToString.Equals("1") Then
                        inputdata = inputdata & "1"
                    ElseIf input(intPos).ToString.Equals("2") Then
                        inputdata = inputdata & "2"
                    ElseIf input(intPos).ToString.Equals("3") Then
                        inputdata = inputdata & "3"
                    ElseIf input(intPos).ToString.Equals("4") Then
                        inputdata = inputdata & "4"
                    ElseIf input(intPos).ToString.Equals("5") Then
                        inputdata = inputdata & "5"
                    ElseIf input(intPos).ToString.Equals("6") Then
                        inputdata = inputdata & "6"
                    ElseIf input(intPos).ToString.Equals("7") Then
                        inputdata = inputdata & "7"
                    ElseIf input(intPos).ToString.Equals("8") Then
                        inputdata = inputdata & "8"
                    ElseIf input(intPos).ToString.Equals("9") Then
                        inputdata = inputdata & "9"                Else
                    End If
                    intPos += 1
                Loop Until intPos >= intLastPos
            End If
            Return inputdata
        End Function    Private Sub btnTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTest.Click
            Dim strAbout As String = String.Empty
            Dim strTest As String
            Dim strEdited As String
            Dim intLastPos As Integer
            'input        'strTest = changeToUp(txtTest.Text)        'strEdited = strTest
            'If strTest.Length <= 0 Then                                      'Try statements
            '    strAbout = "Please enter a string with some letters"
            'Else
            '    'convert string's letters to upper case
            '    strTest = strTest.ToUpper
            'Try        strEdited = changeToUp(txtTest.Text)        ' strEdited = strTest
            'strTest = txtTest.Text
            'strTest = strTest.Trim
            'strTest = strTest.ToUpper
            'strEdited = String.Empty
            ' intLastPos = strTest.Length - 1
            'strEdited = filterOutLettle(strEdited)
            'For intPos As Integer = 0 To intLastPos
            '    Dim chrVar As Char = strTest.Chars(intPos)
            '    If chrVar >= "A" And chrVar <= "Z" Then
            '        strEdited = strEdited & chrVar        '    ElseIf (Int(strTest(intPos).ToString) >= 0) And (Int(strTest(intPos).ToString) <= 9) Then
            '        strEdited = strEdited & strTest(intPos).ToString
            '    Else        '    End If
            ' Next
            'ElseIf strEdited(intPos).ToString.Equals("0") Then
            '    strEdited = strEdited & "0"
            'ElseIf strEdited(intPos).ToString.Equals("1") Then
            '    strEdited = strEdited & "1"
            'ElseIf strEdited(intPos).ToString.Equals("2") Then
            '    strEdited = strEdited & "2"
            'ElseIf strEdited(intPos).ToString.Equals("3") Then
            '    strEdited = strEdited & "3"
            'ElseIf strEdited(intPos).ToString.Equals("4") Then
            '    strEdited = strEdited & "4"
            'ElseIf strEdited(intPos).ToString.Equals("5") Then
            '    strEdited = strEdited & "5"
            'ElseIf strEdited(intPos).ToString.Equals("6") Then
            '    strEdited = strEdited & "6"
            'ElseIf strEdited(intPos).ToString.Equals("7") Then
            '    strEdited = strEdited & "7"
            'ElseIf strEdited(intPos).ToString.Equals("8") Then
            '    strEdited = strEdited & "8"
            'ElseIf strEdited(intPos).ToString.Equals("9") Then
            '    strEdited = strEdited & "9"        'Else
            'Catch ex As FormatException
            'strAbout = "Please enter a string with some letters~~~!"
            ' End Try
            'filter out non-alphabetic characters
            'Dim strEdited As String = String.Empty
            ' strEdited = filterOutLettle(strTest.Length - 1)
            'Dim intLastPos As Integer = strTest.Length - 1
            'For intPos As Integer = 0 To intLastPos
            '    Dim chrVar As Char = strTest.Chars(intPos)
            '    If chrVar >= "A" And chrVar <= "Z" Then
            '        strEdited = strEdited & chrVar
            '    End If
            'Next        If strEdited.Length <= 0 Then
                strAbout = "Please enter a string that contains letters or digits."
            Else
                'build reversed - the reverse of edited string
                Dim strReversed As String = String.Empty
                intLastPos = strEdited.Length - 1
                For intPos As Integer = 0 To intLastPos
                    Dim chrVar As Char = strEdited.Chars(intPos)
                    strReversed = chrVar & strReversed
                Next            If strEdited = strReversed Then
                    strAbout = "It is a palindrome"
                Else
                    strAbout = "It is not a palindrome"
                End If
            End If
            'End If        lblAbout.Text = strAbout
            txtTest.ReadOnly = True
            btnClear.Enabled = True        btnTest.Enabled = False
        End Sub
        'Private Function filterOutLettle(ByVal enterdata As String) As String
        '    Dim intLastPos = enterdata.Length - 1
        '    For intPos As Integer = 0 To intLastPos
        '        Dim chrVar As Char = enterdata.Chars(intPos)
        '        If chrVar >= "A" And chrVar <= "Z" Then
        '            enterdata = enterdata & chrVar    '        ElseIf (Int(enterdata(intPos).ToString) >= 0) And (Int(enterdata(intPos).ToString) <= 9) Then
        '            enterdata = enterdata & enterdata(intPos).ToString
        '        Else    '        End If
        '    Next
        '    Return enterdata
        'End Function
    End Class
      

  2.   

    Public Class FormPalindromeRecognizer    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
            txtTest.Text = String.Empty
            lblAbout.Text = String.Empty
            txtTest.ReadOnly = False
            btnClear.Enabled = False
            btnTest.Enabled = True
            txtTest.Focus()
        End Sub    Private Sub btnTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTest.Click
            Dim strAbout As String = String.Empty
            'Dim strTest As String
            Dim strEdited As String
        
            Try
                strEdited = removeAllButLettersAndDigits(txtTest.Text)
         
                If strEdited.Length <= 0 Then
                    strAbout = "Please enter a string with some letters"
                Else
                    'convert string's letters to upper case
                    'strEdited = removeAllButLettersAndDigits(txtTest.Text)                Dim strReversed As String = String.Empty                strReversed = reverseString(strEdited)                If strEdited = strReversed Then
                        strAbout = "It is a palindrome"
                    Else
                        strAbout = "It is not a palindrome"
                    End If
                End If
                    'End If                lblAbout.Text = strAbout
                    txtTest.ReadOnly = True
                    btnClear.Enabled = True                btnTest.Enabled = False
                ' End If
       Catch ex As Exception
                MessageBox.Show("Please enter a string")
            End Try
            
        End Sub    Public Function removeAllButLettersAndDigits(ByVal changeText As String) As String    
            changeText = changeText.ToUpper            'filter out non-alphabetic characters
            Dim strEdited As String = String.Empty
            Dim into As String
            Dim intLastPos As Integer = changeText.Length - 1
            Dim x = 0
            Do Until x = intLastPos
                into = changeText.Substring(x, x + 1)
                
                If into >= "A" And into <= "Z" Then
                    strEdited = strEdited & into
                    x += 1
                ElseIf Int(into) >= 0 And Int(into) <= 9 Then
                    strEdited = strEdited & into
                    x += 1
                End If
            Loop
            Return strEdited
            'For intPos As Integer = 0 To intLastPos
            '    Dim chrVar As Char = changeText.Chars(intPos)
            '    If chrVar >= "A" And chrVar <= "Z" Then
            '        strEdited = strEdited & chrVar
            '    End If
            'Next        'Dim i As Long
            'changeText = changeText.ToUpper
            '' Ignore everything except letters and numbers
            'i = 1
            'Do
            '    Select Case Mid(changeText, i, 1)
            '        Case "a" To "z", "0" To "9" : i = i + 1
            '        Case Else : changeText = changeText & changeText.Substring(Mid(i, 1))
            '    End Select
            'Loop While i <= Len(changeText)        'Return changeText    End Function    Public Function reverseString(ByVal convertText As String) As String
            Dim max As Integer        Dim TextValue As String = String.Empty
            max = convertText.Length - 1
            Dim output As String
            Do Until max <= 0
                output = convertText.Substring(max, max + 1)            TextValue = output & TextValue
                max -= 1
            Loop        Return TextValue
        End FunctionEnd Class
    现在的思路
      

  3.   

    费事,用正则表达式“[a-zA-Z0-9]”,几行代码而已。