删除字符串中的特定字符

时间:2014-03-19 08:13:57

标签: vb.net special-characters

我有一个代码来检查字符串中是否有任何特殊字符。如果它检测到字符,它将仅删除字符并继续检查。或者,如果您有任何更简单的方法,请帮助我。谢谢

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

1 个答案:

答案 0 :(得分:0)

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