Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hi,!
I want to compare three strings, so far I have tried with compareTo, but it seems it only works with 2 strings.
I hope someone can help me out!
thanks.
Posted

If you want a bool saying that your strings are equal.

C#
bool IsEqualStrings(params string[] Strings)
        {
            for (int i = 1; i < Strings.Length; i++)
            {
                if (Strings[i] != Strings[0])
                    return false;
            }
            return true;
        }


Now you can use as many strings as you like.
Like this
C#
bool Equalstrings = IsEqualStrings("MyString","MyString","MyString","MyString");
 
Share this answer
 
v2
Comments
Thomas Daniels 3-Mar-13 9:22am    
+5!
Per Söderlund 3-Mar-13 9:24am    
Thanks
Jibesh 4-Mar-13 18:32pm    
+5
C++
BOOL 3StringsIdentical(char *s1,char *s2,char *s3)
{
     if(strcmp(s1,s2)==0 && strcmp(s1,s3)==0)
          return TRUE;
     else
          return FALSE;
}


Then just call:
C++
BOOL TheyAreIdentical=3StringsIdentical("abc","abc","abc");

or
C++
BOOL TheyAreIdentical=3StringsIdentical("abc","xxx","abc");
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Mar-13 22:40pm    
I voted one, because it shows some fundamental luck of understanding of very basic programming.

if (something)
return true;
else
return false;


is strictly equivalent to

return something;

Besides, your code is not valid as C# code, but this is less important.

Please think about it: if you give an advice, you should be able to write reasonably good code.

Sorry.
—SA
Per Söderlund 3-Mar-13 8:54am    
I agree, this is bad in many ways.
Sergey Alexandrovich Kryukov 3-Mar-13 12:33pm    
The real problem is that Michael tries to defend himself. Please see the comments below.
—SA
Michael Haephrati 3-Mar-13 3:06am    
I disagree. First, my solution do the job and second, that's the beauty of C / C++ : you can write the same logic in many forms of code. At the same time I could have suggested:
return(!strcmp(s1,s2) && !strcmp(s1,s3));
One line of code.... Very impressive! BUT I am trying to help someone understand.... So, NO. I wouldn't want to use 'return something' as you suggest, but stay with my answer, and that is for instructional purposes. By the way I have been coding for 25 years, and a member here since 2003, so I don't appreciate your implications about my coding skills...
Per Söderlund 3-Mar-13 8:58am    
You are trying to make someone understand C/C++ in a C# question.
here is y solution. scales to any number of strings.
probably performs a bit better than the other alternatives:

C#
bool AreEqualStrings(params string[] Strings)
{
    for (int i = 1; i < Strings.Length; i++)
    {
        if (Strings[i].Length != Strings[0].Length)
            return false;
    }
    for (int i = 0; i < Strings[0].Length; i++)
    {
        for (int j = 1; i < Strings.Length; i++)
        {
            if (Strings[j][i] != Strings[0][i])
                return false;
        }
    }
    return true;
}
 
Share this answer
 
C#
class Program
    {
        static void Main(string[] args)
        {
            string a1 = "aaaaa";
            string a2 = "aaaaa";
            string a3 = "aaaaa";
            string.com
            bool b = CompMethod(a1, a2, a3);

            Console.WriteLine(b.ToString());

        }
        public static bool CompMethod(string a1, string a2, string a3)
        {
            return Convert.ToBoolean(a1 == a2 && a1 == a3);
        }
    }

Source : http://forums.asp.net/t/1687989.aspx/1[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900