1

I want to compare 4 strings if they are equal or not. The problem is that I have to ignore the empty strings, but if they are all empty then the method must also return true, because all empty strings are equal. I could write everything down like

if(string1 != string.Empty && string2 != string.Empty &&
               string1 != string2)
{
   return false
}
if(string1 != string.Empty && string2 != string.Empty &&  string3 != etc....

But I think there is a better way then writing out all the possibilities. But how?

Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
Kimos
  • 99
  • 2
  • 9

5 Answers5

4

This will check that all non-empty or non-null strings are equal:

public static bool CheckNonEmptyStringsForEquality(params string[] strings)
{
    string target = strings.FirstOrDefault(s => !string.IsNullOrEmpty(s));

    if (target == null)
        return false;

    return strings.All(s => string.IsNullOrEmpty(s) || s == target);
}

Use it like this:

Console.WriteLine(
    CheckNonEmptyStringsForEquality("", "X", "X")); // Prints true

Console.WriteLine(
    CheckNonEmptyStringsForEquality("", "X", "Y")); // Prints false

Console.WriteLine(
    CheckNonEmptyStringsForEquality("", "X", "", "X", "", "X", "")); // Prints true

Note: If you want to return true if all strings are null or empty, do this instead:

public static bool CheckNonEmptyStringsForEquality(params string[] strings)
{
    string target = strings.FirstOrDefault(s => !string.IsNullOrEmpty(s));
    return strings.All(s => string.IsNullOrEmpty(s) || s == target);
}

Then this will return true too:

Console.WriteLine(
CheckNonEmptyStringsForEquality("", "", "")); // Prints true
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • didnt work, i am getting false when the array contain one empty string and others strings are equal – Kimos Mar 23 '16 at 09:29
  • @Kimos Post the code that doesn't work and I should be able to explain what you did wrong. If you look at the very first usage example I gave above, you can see that it returns true when the array contains one empty string and the other strings are equal. The code I posted definitely works. – Matthew Watson Mar 23 '16 at 10:04
2

Something like this? Do Distinct and check the Count on an Array of strings.

var strings = new string[] {"str1", "str2", "str3"};

if( (strings.Where(s=>!string.IsNullOrEmpty(s)).Distinct().Count() == 1))
{       
    // unique
}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • 1
    The question is not clear but i understand from the example that we search if "any 2 strings" are equals, not if "all" are equals (meaning `strings.Distinct().Count() < strings.Count()`) – tafia Mar 23 '16 at 09:11
  • Query is correct, `count` will be 1 if we have same string across. – Hari Prasad Mar 23 '16 at 09:12
  • so I guess I didn't understand the question – tafia Mar 23 '16 at 09:18
1

Try this:

    static void Main(string[] args)
    {
        var text1 = new[] { "x", "y", "z", "w" };
        var text2 = new[] { "x", "y", null, "" };
        var text3 = new[] { "x", "x", "x", "x" };
        var text4 = new[] { "x", "x", null, "" };

        MyComparer(text1); // False
        MyComparer(text2); // False
        MyComparer(text3); // True
        MyComparer(text4); // True
    }

    private static bool MyComparer(IEnumerable<string> array)
    {
        return array.Where(t => !string.IsNullOrEmpty(t)).Distinct().Count() == 1;
    }
cani
  • 91
  • 7
  • var text5 = new[] { "", "", "", "" }; returns false, should be true all empty strings are euqal... – Kimos Mar 23 '16 at 09:58
  • I do not think the question states that it should return true or false. If so, it should be described as such. – cani Mar 23 '16 at 11:07
0

if you pass the string in an array,

var collection = new string[] { "India", "", "India", "India" };
var isEqual = collection.Distinct().Contains("") ? collection.Distinct().Count() <= 2 : collection.Distinct().Count() == 1;
Venu prasad H S
  • 231
  • 1
  • 8
0
        //Hope this helps
        string a = "";
        string b = "mystring";
        string c = "mystring";
        string d = "mystring";
        bool isEqual = true;
        ArrayList array = new ArrayList();
        array.Add(a);
        array.Add(b);
        array.Add(c);
        array.Add(d);


        foreach (string stringMemeber in array)
        {
            if (string.IsNullOrEmpty(stringMemeber))
            {
                continue;
            }
            else
            {
                foreach (string NestedStringMember in array)
                {
                    if (string.IsNullOrEmpty(NestedStringMember))
                    {
                        continue;
                    }
                    else
                    {
                        if (string.Compare(stringMemeber,NestedStringMember)!=0)
                        {
                            isEqual = false;
                            break;
                        }
                    }
                }

                if (!isEqual)
                {
                    break;
                }

            }
        }


        Console.WriteLine("The strings are equal: " + isEqual.ToString());
Shadi Zidan
  • 154
  • 5
  • You can check equality of every member in an array using a single loop. There's no need for the second `foreach`: it's just doing extra work. – RoadieRich Mar 23 '16 at 17:17