📜  C#|检查两个StringBuilder对象是否相等

📅  最后修改于: 2021-05-29 22:37:21             🧑  作者: Mango

StringBuilder.Equals方法用于检查此实例是否等于指定的对象。

范例1:

// C# program to if a StringBuilder object
// is equal to another StringBuilder object
using System;
using System.Text;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Create a StringBuilder object
        // with a String passed as parameter
        StringBuilder st1 = new StringBuilder("GeeksforGeeks");
  
        // Checking whether st1 is
        // equal to itself or not
        Console.WriteLine(st1.Equals(st1));
    }
}
输出:
True

范例2:

// C# program to if a StringBuilder object
// is equal to another StringBuilder object
using System;
using System.Text;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Create a StringBuilder object
        // with a String passed as parameter
        StringBuilder st1 = new StringBuilder("GeeksforGeeks");
  
        // Create a StringBuilder object
        // with a String passed as parameter
        StringBuilder st2 = new StringBuilder("GFG");
  
        // Checking whether st1 is
        // equal to st2 or not
        Console.WriteLine(st1.Equals(st2));
  
        // Create a StringBuilder object
        // with a String passed as parameter
        StringBuilder st3 = new StringBuilder();
  
        // Assigning st2 to st3
        st2 = st3;
  
        // Checking whether st3 is
        // equal to st2 or not
        Console.WriteLine(st3.Equals(st2));
    }
}
输出:
False
True

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.text.stringbuilder.equals?view=netcore-2.2