Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking multiple contains on one string

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

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

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?

like image 423
Kevin DiTraglia Avatar asked May 02 '12 17:05

Kevin DiTraglia


2 Answers

Does this win for shortest?

@".,/\".Any(input.Contains)
like image 187
IngisKahn Avatar answered Oct 05 '22 09:10

IngisKahn


How about this?

    if(input.IndexOfAny(new char[] { ',', '/', '\\', '.' })>=0)
    {

    }
like image 45
John Alexiou Avatar answered Oct 05 '22 08:10

John Alexiou