दो StringBuilders की सामग्री की तुलना करने के लिए C# में समान विधि का उपयोग किया जाता है।
हमारे दो StringBuilders निम्नलिखित हैं -
// first
StringBuilder str1 = new StringBuilder();
str1.Append("Tim");
str1.Append("Tom");
str1.Append("Henry");
// second
StringBuilder str2 = new StringBuilder();
str2.Append("John");
str2.Append("David");
str2.Append("Beth"); अब दोनों विधियों की तुलना करने के लिए बराबर () विधि का उपयोग करें -
if (str1.Equals(str2)) {
Console.WriteLine("Contents are equal!");
} निम्नलिखित पूरा कोड है -
उदाहरण
using System;
using System.Text;
class Demo {
static void Main() {
// first
StringBuilder str1 = new StringBuilder();
str1.Append("Tim");
str1.Append("Tom");
str1.Append("Henry");
// second
StringBuilder str2 = new StringBuilder();
str2.Append("John");
str2.Append("David");
str2.Append("Beth");
// check for equality
if (str1.Equals(str2)) {
Console.WriteLine("Contents are equal!");
} else {
Console.WriteLine("Contents are unequal!");
}
}
} आउटपुट
Contents are unequal!