18

I am using an array within a C# program as follows:

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?

4 Answers 4

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

Using .NET 3.0/3.5; you will need a using System.Linq;

Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't work with bytes, the answer with Array.IndexOf ↓ does work.
22

You could use Array.IndexOf method:

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

Comments

10

If I understood correctly, you need to check if c is in x. Then:

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.