2014-03-19 92 views
0

我有一個代碼來檢查字符串中是否有任何特殊字符。如果它檢測到字符,它將只刪除字符並繼續檢查。或者,如果您有更簡單的方法,請協助我。謝謝刪除字符串中的特定字符

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 

回答

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 
+0

Yes..that工作。謝謝 – Luiey