Phương thức Equals được sử dụng trong C # để so sánh nội dung của hai StringBuilders.
Sau đây là hai StringBuilders của chúng tôi -
// 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"); Bây giờ, hãy sử dụng phương thức Equals () để so sánh cả hai phương thức -
if (str1.Equals(str2)) {
Console.WriteLine("Contents are equal!");
} Sau đây là mã hoàn chỉnh -
Ví dụ
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!");
}
}
} Đầu ra
Contents are unequal!