如何检查字符数组中是否存在特定字符

use*_*232 12 .net c# arrays

我在C#程序中使用如下数组:

char[] x = {'0','1','2'};
string s = "010120301";

foreach (char c in s)
{
    // check if c can be found within s
}
Run Code Online (Sandbox Code Playgroud)

如何检查每个char c以查看它是否在字符数组x中找到?

Dan*_*ott 31

if (x.Contains(c))
{
 //// Do Something
}
Run Code Online (Sandbox Code Playgroud)

使用.NET 3.0/3.5; 你需要一个using System.Linq;

善良,


Dar*_*rov 19

您可以使用Array.IndexOf方法:

if (Array.IndexOf(x, c) > -1)
{
    // The x array contains the character c
}
Run Code Online (Sandbox Code Playgroud)


Kon*_*man 10

如果我理解正确,你需要检查c是否在x中.然后:

if(x.Contains(c)) { ... }
Run Code Online (Sandbox Code Playgroud)