C# 检查一个字符串上的多个包含
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10419106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking multiple contains on one string
提问by Kevin DiTraglia
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?
我需要添加一些我想检查的字符,并且想知道是否有更简洁的语法来完成同样的事情?类似于 SQL 的 IN 运算符?
if ( input IN (",", "/", @"\", ....etc ) )
Anybody know of any cool tricks to accomplish this without adding lots of code?
有人知道在不添加大量代码的情况下完成此任务的任何很酷的技巧吗?
采纳答案by empi
Consider using Regex (specify characters you want to check in brackets - remember that some of them must be escaped):
考虑使用正则表达式(在括号中指定要检查的字符 - 请记住其中一些必须转义):
Regex.IsMatch(input, @"[,/]");
or
或者
new[] {",", "/"}.Any(input.Contains)
回答by ja72
How about this?
这个怎么样?
if(input.IndexOfAny(new char[] { ',', '/', '\', '.' })>=0)
{
}
回答by Olivier Jacot-Descombes
Try
尝试
If (input.IndexOfAny(new char[] { ',', '/', '\', '.' }) >= 0) {
...
}
or
或者
If (input.IndexOfAny(@",/\.".ToCharArray()) >= 0) {
...
}
回答by Nicholas Butler
You could use some Linq:
你可以使用一些 Linq:
if ( ",/\.".ToCharArray().Any( c => input.Contains( c ) ) )
回答by Jim Dagg
You could use String.IndexOfAny -- it will scan the string for any one of a set of characters in an array:
您可以使用 String.IndexOfAny - 它会扫描字符串以查找数组中的一组字符中的任何一个:
if (e.Label.IndexOfAny(new char[]{',', '/', @'\', '.' /* other chars here */}) > -1)
回答by IngisKahn
Does this win for shortest?
这是最短的胜利吗?
@".,/\".Any(input.Contains)
回答by user1675905
"asdfasdf".ContainsAny(".","/","4");
public static bool ContainsAny(this string stringToCheck, params string[] parameters)
{
return parameters.Any(parameter => stringToCheck.Contains(parameter));
}
回答by Matthew Alunni
An extension method could make things look clean. Have a look at the following.
扩展方法可以使事情看起来干净。看看下面的内容。
public static bool ContainsChar(this string input, params char[] characters)
{
foreach (var character in characters)
{
if (input.Contains(character))
{
return true;
}
}
return false;
}
The method's parameters are variadic, so you can add as many chars as you want separated by commas. If you're not comfortable using extension methods, modify to the following:
该方法的参数是可变参数,因此您可以添加任意数量的字符,以逗号分隔。如果您不习惯使用扩展方法,请修改为以下内容:
public static bool ContainsChar(string input, params char[] characters)
{
foreach (var character in characters)
{
if (input.Contains(character))
{
return true;
}
}
return false;
}
Example usage follows:
示例用法如下:
string myString = "this is my string";
//extension
if (myString.ContainsChar('.', '*', '%')) //do something
//static method
if (ContainsChar(myString, '.', '*', '%')) //do something

