Can I add String.contains more than one value?

I want to create a control when creating companyId so as not to allow the creation of an identifier with special characters like (&), (/), (), (ñ), ('):

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

But I can not do this:

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

How to check multiple lines on one line?

+4
source share
2 answers

You can use collections such as Char()and Enumerable.Contains. Because it Stringimplements 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 a similar approach is used using Enumerable.Contains:

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

third option using String.IndexOfAny:

If txtIdCompany.Text.IndexOfAny(disallowed.ToCharArray()) >= 0 Then
    ' alert error message
End If
+7
source
If txtIdCompany.Text.Contains("&") Or txtIdCompany.Text.Contains("\") Or txtIdCompany.Text.Contains("/") Then

   // alert error message

 End If 
0

Source: https://habr.com/ru/post/1617285/


All Articles