none
How to get not matched elements from 2 string arrays RRS feed

  • Question

  • I am having 2 string arrays. I want to get not matched elements from Both strings. How to do it, pls help me.

    string[] stringarray1, stringarray2;
    
    stringarray1 = new string[5]{ “Element 1\n”, “Element 2\n”, “Element 3\n”, “Element 4\n”, “Element 5” }; stringarray2 = new string[]{ “Element 2”, “Element 3\n”, “Element 5” };



    in above string array (either in stringarray1 or stringarray2) element may end with '\n' in both. If it's not added at the end of every string element, i need to add, compare and get the not matched elements

    How to achieve it. Please help me

    Thanks

    Sunday, March 1, 2020 5:59 AM

Answers

  • Hi Learning Rocks,

    Thank you for posting here.

    The following code adds "\n" to the end of each element that does not end with "\n".

                for (int i = 0; i < stringarray1.Length; i++)
                {
                    if (!stringarray1[i].EndsWith("\n"))
                    {
                        stringarray1[i] += "\n";
                    }
                }
                for (int i = 0; i < stringarray2.Length; i++)
                {
                    if (!stringarray2[i].EndsWith("\n"))
                    {
                        stringarray2[i] += "\n";
                    }
                }

    What does "match" mean here, do you want to get all the elements that are in stringarray1 but not in stringarray2?

    If so, try the following code.

                string[] combined = stringarray1.Concat(stringarray2).ToArray();
                var query = combined.GroupBy(x => x)
                    .Where(g => g.Count() == 1)
                    .Select(y => y.Key)
                    .ToList();

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 2, 2020 1:59 AM

All replies

  • Hi Learning Rocks,

    To find matching or not matching in two arrays or lists it done by several ways In C# linq you can use Intersect and Except Methods,here is my solution.

    static void Main(string[] args)
            {
    
    
                string[] stringarray1, stringarray2;
    
                stringarray1 = new string[5] { "Element 1\n", "Element 2\n", "Element 3\n", "Element 4\n", "Element 5" };
                
                
                stringarray2 = new string[] { "Element 2", "Element 3\n", "Element 5" };
    
    
    
    
                Console.WriteLine("---------------- stringarray1 Elemets  -----------------");
                foreach (var value in stringarray1)
                {
                    Console.WriteLine(value);
                }
    
    
    
                Console.WriteLine("---------------- stringarray2 Elemets  -----------------");
                foreach (var value in stringarray2)
                {
                    Console.WriteLine(value);
                }
    
    
    
    
                //Return not matching elements between stringarray1 and stringarray2
                List<string> Nointersect = stringarray1.Except(stringarray2).Union(stringarray2.Except(stringarray1)).ToList();
    
                 
    
                Console.WriteLine("---------------- No intersect Elemets  -----------------");
                foreach (var value in Nointersect)
                {
                    Console.WriteLine(value);
                }
    
    
    
                //Return matching elements between stringarray1 and stringarray2
                Console.WriteLine("---------------- Intersect Elemets  -----------------");
                var intersect = stringarray1.Intersect(stringarray2);
    
                foreach (var value in intersect)
                {
                    Console.WriteLine(value);
                }
    
    
    
    
                Console.ReadKey();
            }

    Sunday, March 1, 2020 7:29 AM
  • Hi Learning Rocks,

    Thank you for posting here.

    The following code adds "\n" to the end of each element that does not end with "\n".

                for (int i = 0; i < stringarray1.Length; i++)
                {
                    if (!stringarray1[i].EndsWith("\n"))
                    {
                        stringarray1[i] += "\n";
                    }
                }
                for (int i = 0; i < stringarray2.Length; i++)
                {
                    if (!stringarray2[i].EndsWith("\n"))
                    {
                        stringarray2[i] += "\n";
                    }
                }

    What does "match" mean here, do you want to get all the elements that are in stringarray1 but not in stringarray2?

    If so, try the following code.

                string[] combined = stringarray1.Concat(stringarray2).ToArray();
                var query = combined.GroupBy(x => x)
                    .Where(g => g.Count() == 1)
                    .Select(y => y.Key)
                    .ToList();

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 2, 2020 1:59 AM