Java String contentEquals() Method

Java String contentEquals() Method

What is String?

In Java, an string class is a series of characters that are interpreted literally by a script. An object called a string represents a string of characters. Java.lang is used. A string object is made using the String class.

  • Numerous methods, including compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), and substring(), are available in the Java String class.

To know more about String Class in java read the complete article.

String contentEquals() Method:

The contentEquals() method in Java is a method of the String class that compares the content of a string to the content of another object. The method returns a boolean value indicating whether the content of the two objects are equal or not.

The String’s content can also be compared using the contentEquals() method. Any implementation of the CharSequence interface can be passed as an input to ContentEquals(). Therefore, it is possible to compare a String, StringBuffer, StringBuilder, CharBuffer, or Segment.

  • The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.

Syntax:

public boolean contentEquals(CharSequence cs)

Parameters:

cs - The sequence to compare this String against

Let’s look at a string-related Java program where the Java String contentEquals() method is used to perform an operation on the given string.

Examples : Concatenating two given strings difference

Run
public class Main {
  public static void main(String[] args) {
    String myStr = "Hello PrepInsta ";
    System.out.println(myStr.contentEquals("Hello PrepInsta "));
    System.out.println(myStr.contentEquals("Hello"));
    System.out.println(myStr.contentEquals("PrepInsta"));
  }
}

Output

true
false
false

Examples 2 : Java String Concat() Method

Run
public class Main {
  public static void main(String[] args) {
    String myStr = "PrepInsta Prime ";
    System.out.println(myStr.contentEquals("PrepInsta"));
    System.out.println(myStr.contentEquals("Prime"));
    System.out.println(myStr.contentEquals("PrepInsta Prime "));
  }
}

Output

false
false
true

Examples 3: Concatenating two given strings ignoring lower and upper case.

Run
public class Main {
  public static void main(String[] args) {
    String myStr = "Hello PrepInsta ";
    System.out.println(myStr.contentEquals("Hello Prep "));
    System.out.println(myStr.contentEquals("Hello"));
    System.out.println(myStr.contentEquals("PrepInsta"));
  }
} 

Output

false
false
false

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription