C# में Equals() मेथड का उपयोग यह जांचने के लिए किया जाता है कि दो String ऑब्जेक्ट्स का मान समान है या नहीं।
सिंटैक्स
bool string.Equals(string s1, string s2)
ऊपर, s1 और s2 तुलना किए जाने वाले तार हैं।
उदाहरण
using System; public class Demo { public static void Main(string[] args) { string s1 = "Kevin"; string s2 = "Tom"; string s3 = s2; Console.WriteLine("String1 = "+s1); Console.WriteLine("String2 = "+s2); Console.WriteLine("Are both the strings equal? = "+s1.Equals(s2)); Console.WriteLine("Are both the strings equal? = "+s2.Equals(s3)); } }
आउटपुट
String1 = Kevin String2 = Tom Are both the strings equal? = False Are both the strings equal? = True
उदाहरण
using System; public class Demo { public static void Main(string[] args) { string s1 = "David"; string s2 = "David"; string s3 = s2; string s4 = "Tom"; string s5 = s4; Console.WriteLine("String1 = "+s1); Console.WriteLine("String2 = "+s2); Console.WriteLine("String3 = "+s3); Console.WriteLine("String4 = "+s4); Console.WriteLine("String5 = "+s5); Console.WriteLine("Is s1 and s2 equal? = "+s1.Equals(s2)); Console.WriteLine("Is s2 and s3 equal? = "+s2.Equals(s3)); Console.WriteLine("Is s3 and s4 equal? = "+s3.Equals(s4)); Console.WriteLine("Is s4 and s5 equal? = "+s4.Equals(s5)); } }
आउटपुट
String1 = David String2 = David String3 = David String4 = Tom String5 = Tom Is s1 and s2 equal? = True Is s2 and s3 equal? = True Is s3 and s4 equal? = False Is s4 and s5 equal? = True