检查一个字符串中的多个包含 [英] Checking multiple contains on one string

查看:56
本文介绍了检查一个字符串中的多个包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个当前看起来像这样的条件...

So I have a conditional that currently looks like this...

if (input.Contains(",") || input.Contains("/") || input.Contains(@"\") || input.Contains("."))

我需要添加一些要检查的字符,并且想知道是否有更简洁的语法来完成相同的事情?类似于SQL的IN运算符?

I need to add a few more characters that I want to check for and was wondering if there's a more condensed syntax to accomplish the same thing? Something similar to SQL's IN operator?

if ( input IN (",", "/", @"\", ....etc )  )

任何人都知道在不添加大量代码的情况下即可完成此操作的任何绝妙技巧?

Anybody know of any cool tricks to accomplish this without adding lots of code?

推荐答案

考虑使用Regex(指定要在方括号中输入的字符-请记住其中一些必须转义):

Consider using Regex (specify characters you want to check in brackets - remember that some of them must be escaped):

Regex.IsMatch(input, @"[,/]");

new[] {",", "/"}.Any(input.Contains)

这篇关于检查一个字符串中的多个包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆