equals

expect open operator override fun equals(other: Any?): Boolean(source)

Indicates if other object is equal to this String.

An other object is equal to this String if and only if it is also a String, it has the same length as this String, and characters at the same positions in each string are equal to each other.

Since Kotlin

1.0

Samples

import java.util.Locale
import java.util.regex.Pattern
import kotlin.test.*

fun main() { 
   //sampleStart 
   println("\"\" == \"\" is ${"" == ""}") // true
println("\"abc\" == \"abc\" is ${"abc" == "abc"}") // true

println("\"abc\" == \"abcd\" is ${"abc" == "abcd"}") // false
println("\"abc\" == \"Abc\" is ${"abc" == "Abc"}") // false
// If a character's case doesn't matter, strings could be compared in a case-insensitive manner
println("\"abc\".equals(\"Abc\", ignoreCase = true) is ${"abc".equals("Abc", ignoreCase = true)}") // true

val builder = StringBuilder("abc")
println(builder) // abc
// Although the builder holds the same character sequence, it is not a String
println("\"abc\".equals(builder) is ${"abc".equals(builder)}") // false 
   //sampleEnd
}
actual open operator override fun equals(other: Any?): Boolean(source)

Indicates if other object is equal to this String.

An other object is equal to this String if and only if it is also a String, it has the same length as this String, and characters at the same positions in each string are equal to each other.

Since Kotlin

1.1

Samples

import java.util.Locale
import java.util.regex.Pattern
import kotlin.test.*

fun main() { 
   //sampleStart 
   println("\"\" == \"\" is ${"" == ""}") // true
println("\"abc\" == \"abc\" is ${"abc" == "abc"}") // true

println("\"abc\" == \"abcd\" is ${"abc" == "abcd"}") // false
println("\"abc\" == \"Abc\" is ${"abc" == "Abc"}") // false
// If a character's case doesn't matter, strings could be compared in a case-insensitive manner
println("\"abc\".equals(\"Abc\", ignoreCase = true) is ${"abc".equals("Abc", ignoreCase = true)}") // true

val builder = StringBuilder("abc")
println(builder) // abc
// Although the builder holds the same character sequence, it is not a String
println("\"abc\".equals(builder) is ${"abc".equals(builder)}") // false 
   //sampleEnd
}
actual open operator override fun equals(other: Any?): Boolean(source)

Indicates if other object is equal to this String.

An other object is equal to this String if and only if it is also a String, it has the same length as this String, and characters at the same positions in each string are equal to each other.

Since Kotlin

1.0

Samples

import java.util.Locale
import java.util.regex.Pattern
import kotlin.test.*

fun main() { 
   //sampleStart 
   println("\"\" == \"\" is ${"" == ""}") // true
println("\"abc\" == \"abc\" is ${"abc" == "abc"}") // true

println("\"abc\" == \"abcd\" is ${"abc" == "abcd"}") // false
println("\"abc\" == \"Abc\" is ${"abc" == "Abc"}") // false
// If a character's case doesn't matter, strings could be compared in a case-insensitive manner
println("\"abc\".equals(\"Abc\", ignoreCase = true) is ${"abc".equals("Abc", ignoreCase = true)}") // true

val builder = StringBuilder("abc")
println(builder) // abc
// Although the builder holds the same character sequence, it is not a String
println("\"abc\".equals(builder) is ${"abc".equals(builder)}") // false 
   //sampleEnd
}
actual open operator external override fun equals(other: Any?): Boolean(source)

Indicates if other object is equal to this String.

An other object is equal to this String if and only if it is also a String, it has the same length as this String, and characters at the same positions in each string are equal to each other.

Since Kotlin

1.3

Samples

import java.util.Locale
import java.util.regex.Pattern
import kotlin.test.*

fun main() { 
   //sampleStart 
   println("\"\" == \"\" is ${"" == ""}") // true
println("\"abc\" == \"abc\" is ${"abc" == "abc"}") // true

println("\"abc\" == \"abcd\" is ${"abc" == "abcd"}") // false
println("\"abc\" == \"Abc\" is ${"abc" == "Abc"}") // false
// If a character's case doesn't matter, strings could be compared in a case-insensitive manner
println("\"abc\".equals(\"Abc\", ignoreCase = true) is ${"abc".equals("Abc", ignoreCase = true)}") // true

val builder = StringBuilder("abc")
println(builder) // abc
// Although the builder holds the same character sequence, it is not a String
println("\"abc\".equals(builder) is ${"abc".equals(builder)}") // false 
   //sampleEnd
}
actual open operator override fun equals(other: Any?): Boolean(source)

Indicates if other object is equal to this String.

An other object is equal to this String if and only if it is also a String, it has the same length as this String, and characters at the same positions in each string are equal to each other.

Since Kotlin

1.8

Samples

import java.util.Locale
import java.util.regex.Pattern
import kotlin.test.*

fun main() { 
   //sampleStart 
   println("\"\" == \"\" is ${"" == ""}") // true
println("\"abc\" == \"abc\" is ${"abc" == "abc"}") // true

println("\"abc\" == \"abcd\" is ${"abc" == "abcd"}") // false
println("\"abc\" == \"Abc\" is ${"abc" == "Abc"}") // false
// If a character's case doesn't matter, strings could be compared in a case-insensitive manner
println("\"abc\".equals(\"Abc\", ignoreCase = true) is ${"abc".equals("Abc", ignoreCase = true)}") // true

val builder = StringBuilder("abc")
println(builder) // abc
// Although the builder holds the same character sequence, it is not a String
println("\"abc\".equals(builder) is ${"abc".equals(builder)}") // false 
   //sampleEnd
}
actual open operator override fun equals(other: Any?): Boolean(source)

Indicates if other object is equal to this String.

An other object is equal to this String if and only if it is also a String, it has the same length as this String, and characters at the same positions in each string are equal to each other.

Since Kotlin

1.8

Samples

import java.util.Locale
import java.util.regex.Pattern
import kotlin.test.*

fun main() { 
   //sampleStart 
   println("\"\" == \"\" is ${"" == ""}") // true
println("\"abc\" == \"abc\" is ${"abc" == "abc"}") // true

println("\"abc\" == \"abcd\" is ${"abc" == "abcd"}") // false
println("\"abc\" == \"Abc\" is ${"abc" == "Abc"}") // false
// If a character's case doesn't matter, strings could be compared in a case-insensitive manner
println("\"abc\".equals(\"Abc\", ignoreCase = true) is ${"abc".equals("Abc", ignoreCase = true)}") // true

val builder = StringBuilder("abc")
println(builder) // abc
// Although the builder holds the same character sequence, it is not a String
println("\"abc\".equals(builder) is ${"abc".equals(builder)}") // false 
   //sampleEnd
}