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.
Returns true if this String represents the same sequence of char values as the specified sequence, false otherwise.
Note: String comparison in Java is done using the equals() and contentEquals() methods of the String class.
.
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
Explanation:Here the output comes is true in myStr.contentEquals("Hello PrepInsta ") because true the String represents the same sequence of char values as the specified sequence and rest two it return false
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
Explanation:Here the output comes is true inmyStr.contentEquals("PrepInsta Prime ") because the String represents the same sequence of char values as the specified sequence and returns false in rest two.
Examples 3: Concatenating two given strings ignoring lower and upper case.
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
Explanation:Here the output comes is False inmyStr.contentEquals("PrepInsta Prime ") because the String represents the different sequence of char values as the specified sequence.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment