加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 可以添加一个String.contains多个值吗?

发布时间:2020-12-16 06:33:58 所属栏目:asp.Net 来源:网络整理
导读:我想创建一个控件,当我创建一个companyId时,不允许使用特殊字符创建id,如(),(/),(),(?),(‘): If txtIdCompany.Text.Contains("") Then // alert error message End If 但我不能这样做: If txtIdCompany.Text.Contains("","/","") Then // alert error me
我想创建一个控件,当我创建一个companyId时,不允许使用特殊字符创建id,如(&),(/),(),(?),(‘):

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

但我不能这样做:

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

如何检查同一行中的多个字符串?

解决方法

您可以使用Char()和Enumerable.Contains之类的集合.由于String实现了IEnumerable(Of Char),即使这种简洁高效的LINQ查询也能工作:

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

这是使用Enumerable.Contains的类似方法:

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

使用String.IndexOfAny的第三个选项:

If txtIdCompany.Text.IndexOfAny(disallowed.ToCharArray()) >= 0 Then
    ' alert error message
End If

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读