Make a Program which is Compare Strings in Kotlin Program ?

  Kotlin Interview Q&A

In this article you will learn today how to compare two wires in Kotlin.

Method 1: Compare two strings using equals()

fun main(args: Array<String>) {

    val style = "Bold"
    val style2 = "Bold"

    if (style.equals(style2))
        println("Equal")
    else
        println("Not Equal")
}

Result

Equal

Method 2 : Compare two strings

fun main(args: Array<String>) {

    val style = "Bold"
    val style2 = "Bold"

    if (style == style2)
        println("Equal")
    else
        println("Not Equal")
}

Result

Equal

Method 3: Compare two strings using === (Doesn’t work)

fun main(args: Array<String>) {

    val style = buildString { "Bold" }
    val style2 = buildString { "Bold" }

    if (style === style2)
        println("Equal")
    else
        println("Not Equal")
}

Result

Not Equal

LEAVE A COMMENT