Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is possible to add a String.contains more than one value?

I want to make a control, when I create a companyId, to not permit to create id with special characters like, (&), (/), (), (ñ), ('):

 If txtIdCompany.Text.Contains("&") Then
   // alert error message
 End If 

But I can't do this:

If txtIdCompany.Text.Contains("&", "/", "\") Then
       // alert error message
     End If 

How can I check more than one string in the same line?

like image 687
Esraa_92 Avatar asked Mar 14 '23 02:03

Esraa_92


1 Answers

You can use collections like a Char() and Enumerable.Contains. Since String implements IEnumerable(Of Char) even this concise and efficient LINQ query works:

Dim disallowed = "&/\"
If disallowed.Intersect(txtIdCompany.Text).Any() Then
    ' alert error message
End If

here's a similar approach using Enumerable.Contains:

If txtIdCompany.Text.Any(AddressOf disallowed.Contains) Then
    ' alert error message
End If

a third option using String.IndexOfAny:

If txtIdCompany.Text.IndexOfAny(disallowed.ToCharArray()) >= 0 Then
    ' alert error message
End If
like image 171
Tim Schmelter Avatar answered Mar 17 '23 03:03

Tim Schmelter