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

时间:2009-11-30 08:57:59

标签: c# .net arrays

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

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

foreach (char c in s)
{
    // check if c can be found within s
}

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

4 个答案:

答案 0 :(得分:28)

if (x.Contains(c))
{
 //// Do Something
}

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

善,

答案 1 :(得分:19)

您可以使用Array.IndexOf方法:

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

答案 2 :(得分:10)

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

if(x.Contains(c)) { ... }

答案 3 :(得分:0)

string input = "A_123000544654654"; 
string pattern = "[0-9]+";
System.Text.RegularExpressions.Regex.IsMatch(input, pattern);