📜  Java String contentEquals() 方法及示例

📅  最后修改于: 2022-05-13 01:55:47.136000             🧑  作者: Mango

Java String contentEquals() 方法及示例

String类的contentEquals()方法用于比较字符串。 Java.lang.String 中有两种类型的 contentEquals 方法可用,具有不同的参数:

  1. contentEquals(StringBuffer 某人)
  2. contentEquals(CharSequence cs)

1. contentEquals(StringBuffer sb):

contentEquals(StringBuffer sb) 方法将字符串与指定的 StringBuffer 进行比较。如果 String 表示的字符序列与指定的 StringBuffer 相同,则返回 true;否则,它将返回 false。

句法:

public boolean contentEquals(StringBuffer sb)

返回类型:它有一个布尔返回类型,如果此 String 表示与指定 StringBuffer 相同的字符序列,则返回 true,否则返回 false。

方法参数:有一个StringBuffer类型的参数

如何调用 contentEquals(StringBuffer sb) 方法?

第一步:首先,创建一个StringBuffer类的实例来比较它的字符序列

StringBuffer stringBuffer = new StringBuffer( "GFG is the best");

第 2 步:创建 String 的实例,然后调用其 contentEquals 方法

String str= "GFG is the best";
str.contentEquals(stringBuffer)

下面的Java程序将说明 contentEquals(StringBuffer sb) 方法的使用:

Java
// Java program to demonstrate the working
// of the contentEquals(StringBuffer sb) method
  
import java.io.*;
import java.lang.*;
  
class GFG {
    public static void main(String[] args)
    {
        // creating instance of StringBuffer class
        StringBuffer stringBuffer
            = new StringBuffer("GFG is a portal for geeks");
  
        String one = "GFG is a portal for geeks";
        String two = "GFG is a portal for gamers";
  
        // invoking contentEquals method
        // for String one and two
        System.out.println(
            "String one equals to specified StringBuffer : "
            + one.contentEquals(stringBuffer));
  
        System.out.println(
            "String two equals to specified StringBuffer : "
            + two.contentEquals(stringBuffer));
    }
}


Java
// Java program to demonstrate the working
// of contentEquals(CharSequence cs) metgod
  
import java.io.*;
import java.lang.*;
  
class GFG {
    public static void main(String[] args)
    {
        // creating instance of StringBuffer class
        CharSequence cs
            = "GFG is best website for programmer";
        
        String one = "GFG is best website for programmer";
        String two = "GFG is a portal for geeks";
        
        // invoking contentEquals method 
        // for String one and two
        System.out.println(
            "String one equals to specified sequence : "
            + one.contentEquals(cs));
        
        System.out.println(
            "String two equals to specified sequence : "
            + two.contentEquals(cs));
    }
}


输出
String one equals to specified StringBuffer : true
String two equals to specified StringBuffer : false

2. contentEquals(CharSequence cs)

contentEquals(CharSequence cs) 方法将字符串与指定的 CharSequence 进行比较。如果 String 表示与指定 CharSequence 相同的 char 值序列,则返回 true,否则返回 false。

句法:

public boolean contentEquals(CharSequence cs)

方法返回类型:它有一个布尔返回类型,如果此String表示与指定序列相同的char值序列,则返回true,否则返回false。

参数:它有一个 CharSequence 类型的参数

如何调用 contentEquals(CharSequence cs) 方法?

第1步:首先,创建一个序列来比较char值的序列

CharSequence cs = "portal for geeks"

第 2 步:创建 String 的实例,然后调用其 contentEquals 方法

String str= "portal for geeks";
str.contentEquals(cs);

下面的Java程序将说明 contentEquals(CharSequence cs) 方法的使用 -

例子:

Java

// Java program to demonstrate the working
// of contentEquals(CharSequence cs) metgod
  
import java.io.*;
import java.lang.*;
  
class GFG {
    public static void main(String[] args)
    {
        // creating instance of StringBuffer class
        CharSequence cs
            = "GFG is best website for programmer";
        
        String one = "GFG is best website for programmer";
        String two = "GFG is a portal for geeks";
        
        // invoking contentEquals method 
        // for String one and two
        System.out.println(
            "String one equals to specified sequence : "
            + one.contentEquals(cs));
        
        System.out.println(
            "String two equals to specified sequence : "
            + two.contentEquals(cs));
    }
}
输出
String one equals to specified sequence : true
String two equals to specified sequence : false