2016-12-25 23 views
0

次のコードが動作します。テキストに複数の文字が含まれていないか確認してください。@

Dim aaa As String = "[email protected]" 
    If aaa.Contains("@") Then 
     MsgBox("Error1") 
    End If 

次のコードは機能しません。

Dim bbb As String = "[email protected]@cc" 
    If bbb.Contains("*@*@*") Then 
     MsgBox("Error2") 
    End If 

したがって、次の行にはどのような問題がありますか?文字列bbb文字*@*@*の列が含まれていないためである

If bbb.Contains("*@*@*") Then 

答えて

2

String.Containsはワイルドカード文字をサポートしていません。

これを代わりに使用できます。

If Regex.Match(bbb,".*@.*@.*").Success then 
    MsgBox("Error2") 
End If 
1

ワイルドカードは使用できません。

Containsは*ワイルドカードをサポートしていません。多分これを試してみませんか?

If bbb.Contains("@") And bbb.IndexOf("@") <> bbb.LastIndexOf("@") Then 
    ... 
End If 
0

あなたは@の発生を決定し、それに応じてメッセージを表示するように.count()を使用することができます。

Dim bbb As String = "[email protected]@cc" 
    Dim cnt As Integer 

    cnt = bbb.Count(Function(x) x = "@") 'Number of @ in the string 

    If cnt = 1 Then 
    MsgBox("Error1") 
    ElseIf cnt = 2 Then 
    MsgBox("Error2") 
    End If 

注:私はOPは、文字列内の文字はなく、

を従うべき特定のパターンの出現を探していることを前提としてい
関連する問題