简体   繁体   中英

compare two string array values in C#

I have two string array:

string[] array1 = {"5", "4", "2"};    
string[] array2 = {"1", "2", "3"};

I just want to compare

"5" of array1 to "1" of array2

then "4" of array1 to "2" of array2

then "2" of array1 to "3" of array2

and do some calculation in their foreach condition

here is my code:

  foreach(var items in array1)
  {
      foreach (var ind in array2)
      {
          int a = (Convert.ToInt32(items) - Convert.ToInt32(ind));
          string b = Convert.ToString(a);
          string removeValue = b.Replace("-", String.Empty);
          if (Convert.ToInt32(items) > Convert.ToInt32(ind))
          {
              mp += Negative + ",";
              mpValue = mp.Split(',');
              np += a +",";
              nposition = np.Split(',');
          }
          else
          {
              mp += Positive + ",";
              mpValue = mp.Split(',');
              np += removeValue + ",";
              nposition = np.Split(',');
          }
    } 
}

Correct me if i am wrong..Thanks in advance !

Your looping is wrong. For each item in array1 you are processing every item in array2 . Assuming that both arrays are the same length you want something like this:

for(int i = 0; i < array1.Length; i++)
{
  var items = array1[i];
  var ind = array2[i];

  int a = (Convert.ToInt32(items) - Convert.ToInt32(ind));
  // Rest of code
}

Try for loop instead of foreach ,

string[] array1 = { "5", "4", "2" };
string[] array2 = { "1", "2", "3" };

for (int i=0; i < array1.Length; i++)
{
    int a = (Convert.ToInt32(array1[i]) - Convert.ToInt32(array2[i]));
    string b = Convert.ToString(a);
    string removeValue = b.Replace("-", String.Empty);

    if (Convert.ToInt32(array1[i]) > Convert.ToInt32(array2[i]))
    {
        mp += Negative + ",";
        mpValue = mp.Split(',');
        np += a + ",";
        nposition = np.Split(',');
    }
    else
    {
        mp += Positive + ",";
        mpValue = mp.Split(',');
        np += removeValue + ",";
        nposition = np.Split(',');
    }
}

Or(same thing)

string[] array1 = { "5", "4", "2" };
string[] array2 = { "1", "2", "3" };

for (int i=0; i<array1.Length; i++)
{
    int arr1Ele = Convert.ToInt32(array1[i]);
    int arr2Ele = Convert.ToInt32(array2[i]);

    int a = arr1Ele - arr2Ele;

    string b = Convert.ToString(a);
    string removeValue = b.Replace("-", String.Empty);

    if (arr1Ele > arr2Ele)
    {
        mp += Negative + ",";
        mpValue = mp.Split(',');
        np += a + ",";
        nposition = np.Split(',');
    }
    else
    {
        mp += Positive + ",";
        mpValue = mp.Split(',');
        np += removeValue + ",";
        nposition = np.Split(',');
    }
}

I hope that the calculation part of your code is correct.

You should not do two different loops, because that way you are comparing every object in array1 with every object in array2

Instead, you can use the index and iterate both arrays at the same time. Try this in here: https://dotnetfiddle.net/XKd7zu

    string[] array1 = {"5", "4", "2"};
    string[] array2 = {"1", "2", "3"};

    for (int index = 0; index < array1.GetUpperBound(0); index++)
    {
        // In here I select the object from array 1 in position "index" that 
        // will be compared to the object of array2 in the same possition
        int valueArray1 = Int32.Parse(array1[index]);
        int valueArray2 = Int32.Parse(array2[index]);

        if (valueArray1 > valueArray2)
        {
            Console.WriteLine($"{valueArray1} is greater than {valueArray2}");
        }
        if (valueArray1 < valueArray2)
        {
            Console.WriteLine($"{valueArray2} is greater than {valueArray1}");
        }
        else
        {
            Console.WriteLine($"{valueArray2} is equal than {valueArray1}");
        }
    }

Using one for loop instead: (assuming array1 and array2 have equal lenghts)

for( int i = 0; i < array1.Length; i++ )
{
    int a = int.Parse(array1[i]);
    int b = int.Parse(array2[i]);

    // vv no string-operations needed to get an absolute value
    int diff = Math.Abs(a-b); // your "removeValue"

    if( a > b)
    {
        // ...
    }
    else
    {
        // ...
    }
}

Your code compared each item of array1 to each item in array2. That's because you used two nested loops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM