What is the String.contentEquals method in Java?

What is the String.contentEquals method in Java? Answer

The contentEquals method is used to check if the content of the string is equal to the passed CharSequence or StringBuffer.

Syntax

string.contentEquals(StringBuffer sb)
string.contentEquals(CharSequence cs)

Parameters

  • The contentEquals method expects a StringBuffer or CharSequence as an arguemnt.
  • Any class that implements the CharSequence class can also be passed as an argument to the contentEquals method. Examples include CharBufferSegmentStringStringBuffer, and StringBuilder.

Return value

The contentEquals method returns true if the content of the string and the passed StringBuffer/CharSequence are equal. Otherwise, it returns false.

Code

class ContentEquals {
    public static void main( String args[] ) {
      String str = "Java";
      StringBuffer sb= new StringBuffer("Java");
      StringBuffer sb2= new StringBuffer("JavaScript");

      System.out.println( "str = " + str);
      System.out.println( "sb = " + sb);
      System.out.println( "sb = " + sb2);


      System.out.print( "\nCallingn str.contentEquals(sb) ");
      System.out.println(str.contentEquals(sb));

      System.out.print( "\nCallingn str.contentEquals(sb2) ");
      System.out.println(str.contentEquals(sb2));

    }
}

Explanation

In the code above, we have:

  • Created a:
    • str string with the value Java.
    • sb* StringBuffer with the value Java .
    • Another sb2 StringBuffer with the value JavaScript.
  • Called the contentEquals method on the str object by passing sb as an argument.
  • The contentEquals method will compare the string str to the specified StringBuffer sb. The return value is true if and only if str represents the same sequence of characters as the specified StringBuffer sb. In our case, both the string and StringBuffer have the same sequence of characters, so we get true as a return value.
  • Called the contentEquals method on the str object by passing the sb2 as an argument. In this case, str‘s value Java is not equal to sb2‘s value JavaScript, so false is returned.
What is the String.contentEquals method in Java? Review:

In our experience, we suggest you solve this What is the String.contentEquals method in Java? and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the What is the String.contentEquals method in Java?

Find on Educative

Conclusion:

I hope this What is the String.contentEquals method in Java? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.