how to compare string in C#
C#
4
Posts
3
Posters
18
Views
1
Watching
-
string s1 = "Hello"; string s2 = "Goodbye"; if (s1 == s2) { // strings are equal. }
-
string s1 = "Hello"; string s2 = "Goodbye"; if (s1 == s2) { // strings are equal. }
-
That does not work in C#. You need to do this: if (s1.CompareTo(s2)==0) { } 0 they are equal -1 s1 < s2 1 s1 > s2 check the help file
Comparison of strings in C# is value comparison. if I say: if (s1 == s2) that will be true if the strings have the same contents. If I want reference comparison, I need to use: if ((object) s1 == (object) s2) That forces the compiler to use object.Equals() rather than String.Equals(). In languages that don't support operator overloading, you'd use: if (String.Equals(s1, s2))