比较两个字符串构建器的内容
C# 中使用 Equals 方法比较两个字符串构建器的内容。
以下是我们两个字符串构建器 -
// 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");现在使用 Equals() 方法比较两个方法 -
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!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP