2
string[] a = { 2; a; 3; b; 4; c}
string[] b = { 2; a; 6; c}

I want to compare this two arrays and remove the all digit(2,3,4,6)values and store match string values in another array.

I want the result like this:

string[] c = {a; c;}

I was tried this but it was adding all the values.

string[] result = a.Union(b).ToArray();
1
  • 1
    Union does NOT work like that. Use Intersect Commented May 25, 2017 at 6:49

3 Answers 3

4

Use Intersect and Where:

string[] result = a.Intersect(b).Where(c => !char.IsDigit(c[0])).ToArray();

Based on your comment to remove or replace the colon (;):

string[] result = a.Intersect(b).Where(c => !char.IsDigit(c[0]))
                                .Select(c => c.Replace(';',' ')).ToArray();
Sign up to request clarification or add additional context in comments.

1 Comment

How to remove or replace the colon (;) form string array.
0

you need Intersect not Union

string[] result  = a.Intersect(b).ToArray();

Comments

-1

The easiest solution would be to use Linq to do the job

int tmp;
var c = a.Where(x => b.Contains(x) && !int.TryParse(x, out tmp)).ToArray();

2 Comments

No. It couldn't fix it since it has a ; at the end of the digits. So the !int.TryParse(2;, out tmp) will evaluate to false and it returns it as result too.
notation of this list is invalid C# structure. there should be , in place of ; so i tougth it's typo error. no matter what would you like to achive, "123" is a number, "123;" is a string.

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.