C# 如何检查字符数组中是否存在特定字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1818611/
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
How to check if a particular character exists within a character array
提问by user397232
I am using an array within a C# program as follows:
我在 C# 程序中使用数组,如下所示:
char[] x = {'0','1','2'};
string s = "010120301";
foreach (char c in s)
{
// check if c can be found within s
}
How do I check each char c to see if it is found within the character array x?
如何检查每个字符 c 以查看它是否在字符数组 x 中找到?
采纳答案by Daniel Elliott
if (x.Contains(c))
{
//// Do Something
}
Using .NET 3.0/3.5; you will need a using System.Linq;
使用.NET 3.0/3.5;你需要一个using System.Linq;
Kindness,
善良,
Dan
担
回答by Darin Dimitrov
You could use Array.IndexOfmethod:
您可以使用Array.IndexOf方法:
if (Array.IndexOf(x, c) > -1)
{
// The x array contains the character c
}
回答by Konamiman
If I understood correctly, you need to check if c is in x. Then:
如果我理解正确,您需要检查 c 是否在 x 中。然后:
if(x.Contains(c)) { ... }
回答by serhio
string input = "A_123000544654654";
string pattern = "[0-9]+";
System.Text.RegularExpressions.Regex.IsMatch(input, pattern);