none
Compare two string Arrays

    Question

  • string[] array1= new string[]{"Data","Account","credit","Debit"};//This array Contain constant vlues
    String[] array2 = new string[]{"Data1","Account1","credit","Debit"};//This array may be same or may be diff
    i have to comapre this two arrays ...if two string arrays values same 
    //i have to add this below code..
    string row1="Date";
    Datagridview1.Rows.add(row1);
    string row2="Account";
    Datagridview1.Rows.add(row2);
    string row3="Credit";
    Datagridview1.Rows.add(row3);
    string row4="Debit";
    Datagridview1.Rows.add(row4);
    //if two string arrays values Diff..     i have to add below code
    string row1="Date";
    Datagridview1.Rows.add(row1);
    string row2="Account";
    Datagridview1.Rows.add(row2);
    string row3="Ammount";
    Datagridview1.Rows.add(row3);

    kavitha
    Wednesday, February 01, 2012 1:46 AM

Answers

All replies

  • Is the order of elements in array important? If so, you can just put a small loop to compare elements in array.

    If not, I found the code listed in here neat.


    • Edited by cheong00 Wednesday, February 01, 2012 2:08 AM
    Wednesday, February 01, 2012 2:08 AM
  • please try this, use linq

    string[] array1 = new string[] { "Data", "Account", "credit", "Debit" };//This array Contain constant vlues
    string[] array2 = new string[] { "Data1", "Account1", "credit", "Debit" };//This array may be same or may be diff
    string[] array3 = new string[] { "Data", "Account", "credit", "Debit" };//This array Contain constant vlues
    //i have to comapre this two arrays ...if two string arrays values same 
    //i have to add this below code..
    var diff = from item in array1 where array2.Contains(item) select item;
    if (diff.Count() == array1.Length )
    {
    	//the same
    	MessageBox.Show("array1 vs array2 the same");
    }
    else
    {
    	//not the same
    	MessageBox.Show("array1 vs array2 not the same");
    }
    var diff2 = from item in array1 where array3.Contains(item) select item;
    if (diff2.Count() == array1.Length)
    {
    	//the same
    	MessageBox.Show("array1 vs array3 the same");
    }
    else
    {
    	//not the same
    	MessageBox.Show("array1 vs array3 not the same");
    }
    



    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/
    • Proposed as answer by Sarathi R Wednesday, February 01, 2012 4:44 AM
    Wednesday, February 01, 2012 2:08 AM
  • Hi kavitha Reddy Neeru,
    Order is important, because if array elements are not in the same indexed order, then they are absolutely not equal. You can write a method similar to this that does index-by-index checking.

     

    namespace ConsoleApplication1 {
        class Program {
            static void Main(string[] args) {
                string[] arr0 = { "One", "Two", "Three" };
                string[] arr1 = { "Three", "Two", "One" };
                string[] arr2 = { "One", "Two", "Three" };
    
                bool assert0 = ArraysAreEqual(arr0, arr1);
                bool assert1 = ArraysAreEqual(arr0, arr2);
    
                Debug.WriteLine(assert0); // should print false
                Debug.WriteLine(assert1); // should print true
            }
    
            static bool ArraysAreEqual(string[] x, string[] y) {
                if (x.Length != y.Length) {
                    return false;
                }
    
                for (int index = 0; index < x.Length; index++) {
                    if (x[index] != y[index]) {
                        return false;
                    }
                }
    
                return true;
            }
        }
    }
    

     


    Wednesday, February 01, 2012 3:38 AM
  • Hi Kavitha Reddy,

    Above given two solutions are also good but, we should concentrate for some generic solution that will be useful for any type of array. So please use as given below.

     

    static bool ArraysEqual<T>(T[] a1, T[] a2)

    {

        if (ReferenceEquals(a1,a2))

            return true;

        if (a1 == null || a2 == null)

            return false;

        if (a1.Length != a2.Length)

            return false;

        EqualityComparer<T> comparer = EqualityComparer<T>.Default;

        for (int i = 0; i < a1.Length; i++)

        {

            if (!comparer.Equals(a1[i], a2[i])) return false;

        }

        return true;

    }

    Thanks and Regards,

    Rakesh Aggarwal

    • Proposed as answer by Spartan_578 Wednesday, February 01, 2012 6:57 AM
    Wednesday, February 01, 2012 4:20 AM
  • hi,

    refer the thread below,

    Best way to compare two strings[] in C#


    Don't forget to mark the post as answer or vote as helpful if it does, Regards - Rajasekhar.R
    Wednesday, February 01, 2012 7:12 AM
  • You can also use

     array1.SequenceEqual(array2)

    • Proposed as answer by cheong00 Wednesday, February 01, 2012 8:59 AM
    • Marked as answer by kavitha Reddy Neeru Wednesday, February 01, 2012 9:36 AM
    Wednesday, February 01, 2012 8:49 AM
  • HI,

    You can use the following code to compare the 2 arrays and display the non-equal values in the array.

     

    string[] roles = {"a", "e", "f", "h"};
    string[] allRoles = {"a", "b", "c", "d", "e", "f", "g", "h", "i"};
    
    foreach (string nextRole in allRoles) {
        if(Array.IndexOf(roles, nextRole) == -1) {
            Response.Write(nextRole + "<br/>");
        }
    }
    

     


    Don't forget to mark the post as answer or vote as helpful if it does, Regards - Rajasekhar.R
    • Edited by Rajasekhar.R Wednesday, February 01, 2012 8:55 AM
    Wednesday, February 01, 2012 8:54 AM
  • Nice. Not aware that this method exist. :P
    Wednesday, February 01, 2012 9:00 AM
  • Enumerable.SequenceEqual Method

    Determines whether two sequences are equal according to an equality comparer.

    Careful sequences!

    string[] array1 = new string[] { "Data", "Account", "credit", "Debit" };
    string[] array3 = new string[] { "Account", "credit", "Debit", "Data"};

    array1.SequenceEqual(array3); //return false

     


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/
    Wednesday, February 01, 2012 10:42 AM
  • Thanks to all ..
    kavitha
    Wednesday, February 01, 2012 11:27 AM
  • you also can use this:

    string[] array1 = new string[] { "Data", "Account", "credit", "Debit" }; 
    string[] array2 = new string[] { "Data1", "Account1", "credit", "Debit" }; 
    string[] array3 = new string[] { "Account", "credit", "Debit", "Data"};
    string[] array4 = new string[] { "Data", "Account", "credit", "Debit" }; 
    
    bool isarray12thesame = (array1.Except(array2).Count() == 0); //false
    bool isarray13thesame = (array1.Except(array3).Count() == 0); //true
    bool isarray14thesame = (array1.Except(array4).Count() == 0); //true
    



    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/
    Thursday, February 02, 2012 6:46 AM