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

2021年5月10日15:56:45 发表评论 242 次浏览

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

语法:公共布尔等于(System.Text.StringBuilder sb);在此, sb是要与此实例进行比较的对象, 或者为null。返回值:如果此实例和sb具有相等的字符串, Capacity和MaxCapacity值, 则返回true;否则返回true。否则为假。

示例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( "lsbin" );
  
         //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( "lsbin" );
  
         //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/en-us/dotnet/api/system.text.stringbuilder.equals?view=netcore-2.2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: