检查文本是否包含多个@

问题描述:

以下代码工作。检查文本是否包含多个@

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 

那么,下面一行有什么问题?

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

这是因为该字符串bbb不包含字符*@*@*的序列。

String.Contains不支持通配符。

你可以用它来代替。

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

您不能使用通配符。

包含不支持*通配符。也许试试这个?

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

您可以使用.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在字符串寻找人物的出现,而不是特定的模式应遵循