Pergunta

I have a code to check whether is there any special characters in a string. If it detected the characters, it will remove the chars only and proceed checking. Or if you have any simpler way, please assist me. Thank you

Public Function IsThereAnySpecialCharacter(ByVal str As String) As Integer
        Dim illegalChars As Char() = "~`|@#$^*{}'[]""_<>\".ToCharArray()
        Dim count As Integer
        Try
            count = 0
            For Each ch As Char In str ' to check whether special character exist in a string
                If Not Array.IndexOf(illegalChars, ch) = -1 Then
                    xxxxxxxxxxxxxxxxx ' I want to remove the chars here
                    count = count + 1
                End If
            Next
            Return count
        Catch ex As Exception
            strErrMsg = "Oops! Something is wrong with verify special characters at IsThereAnySpecialCharacter"
            MessageBox.Show(strErrMsg & vbCrLf & "Err: " & ex.Message)
        End Try
    End Function
Foi útil?

Solução

Public Function IsThereAnySpecialCharacter(ByVal str As String) As Integer
    Dim illegalChars As Char() = "~`|@#$^*{}'[]""_<>\".ToCharArray()

    Try
        For Each ch As Char In illegalChars
            str = str.Replace(ch, "")
        Next
        MsgBox(str)
    Catch ex As Exception
        strErrMsg = "Oops! Something is wrong with verify special characters at IsThereAnySpecialCharacter"
        MessageBox.Show(strErrMsg & vbCrLf & "Err: " & ex.Message)
    End Try
    Return 0
End Function
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top