Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 1.49 KB

File metadata and controls

35 lines (27 loc) · 1.49 KB

Java - String contentEquals()方法

原文: https://beginnersbook.com/2013/12/java-string-contentequals-method-example/

方法contentEquals()StringString Buffer进行比较并返回一个布尔值。如果StringString缓冲区匹配,则返回true,否则返回false

boolean contentEquals(StringBuffer sb)

在这个例子中,我们有两个字符串和两个字符串缓冲区。我们使用contentEquals()方法比较字符串和字符串缓冲区。这里我们通过直接调用System.out.println语句中的方法来显示结果。但是,您也可以将返回的值存储在布尔变量中,并进一步使用它:boolean var = str1.contentEquals(sb1);

public class ContentEqualsExample {
   public static void main(String args[]) {
       String str1 = "First String";
       String str2 = "Second String";
       StringBuffer str3 = new StringBuffer( "Second String");
       StringBuffer str4 = new StringBuffer( "First String");
       System.out.println("str1 equals to str3:"+str1.contentEquals(str3));
       System.out.println("str2 equals to str3:"+str2.contentEquals(str3));
       System.out.println("str1 equals to str4:"+str1.contentEquals(str4));
       System.out.println("str2 equals to str4:"+str2.contentEquals(str4));
   }
}

输出:

str1 equals to str3:false
str2 equals to str3:true
str1 equals to str4:true
str2 equals to str4:false