Scala String contentEquals() Method with Example

Here, we will learn about the contentEquals() method of String in Scala. It is used to check whether the string and string Buffer contain the same values or not. We will see its working, syntax and example.
Submitted by Shivang Yadav, on February 04, 2021

String is an immutable collection that stores sequences of characters.

String contentEquals() Method

The contentEquals() method on strings is used to compare the contents of a string and a string buffer. It checks for the equality of both.

Syntax:

string_Name.contentEquals(StringBuffer string_buffer)

Parameters: The method accepts a single parameter which a stringBuffer to be checked for equality with the string.

Return Value: It returns a Boolean value which is the result of the comparison operation.

Example 1:

object myObject {
    def main(args: Array[String]) {
        val myString = "Scala"
        val myStringBuffer = new StringBuffer("Scala")
        
        val hasSameContent = myString.contentEquals(myStringBuffer)
    
        if(hasSameContent)
            println("The string and stringBuffer have the same content!")
        else 
            println("The string and stringBuffer have different content!")
    }
}

Output:

The string and stringBuffer have the same content!

Example 2:

object myObject {
    def main(args: Array[String]) {
        val myString = "Scala"
        val myStringBuffer = new StringBuffer("Scala programming language")
        
        val hasSameContent = myString.contentEquals(myStringBuffer)
    
        if(hasSameContent)
            println("The string and stringBuffer have same content!")
        else 
            println("The string and stringBuffer have different content!")
    }
}

Output:

The string and stringBuffer have different content!


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.