- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin String - equals() Function
The Kotlin string equals() function is used to compare the equality of two objects. It checks whether the specified object is "equal to" the current string.
It is available in the String class as part of the Any class implementation, and the implementation must meet the following requirements:
- Reflexive: for any non-null value x, x.equals(x) should return true.
- Symmetric: for any non-null values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
- Transitive: for any non-null values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
- Consistent: for any non-null values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided that none of the information used in the equals comparison on the objects is modified.
- Never equal to null: for any non-null value x, x.equals(null) should return false.
Syntax
Following is the syntax of the Kotlin string equals() function −
fun equals(other: Any?): Boolean
Parameters
This function accepts another object as a parameter, which represents a string that needs to be compared with the current string.
Return value
This function return true if the strings are equal, otherwise returns false.
Example 1: Compare Current String with Another
Following is the basic example, here how we can compare the current string with another string using equals() in Kotlin −
fun main() {
val string1 = "Kotlin"
val string2 = "Kotlin"
// Comparing strings with equals
println("Is both string equal: ${string1.equals(string2)}")
}
Output
Following is the output −
Is both string equal: true
Example 2: Compare with a Null Value or Non-String
Now, let's look at another example of the equals() function what if the current string compares with a null value or non-string object −
fun main() {
val string1 = "Kotlin"
// Comparing with a null value
println("Is this string equals: ${string1.equals(null)}")
// Comparing with a non-string object
println("Is this string equals: ${string1.equals(123)}")
}
Output
If the index is out of bound −
Is this string equals: false Is this string equals: false
Example 3: Compare with Empty String
In this example, this function returns false. If theequals()function compares with an empty string object −
fun main() {
val str1 = "Aman"
val str2 = ""
val res = str1.equals(str2)
println("Is both string equal: ${res}")
}
Output
Following is the output −
Is both string equal: false