Compare String Kotlin

Viewed 64347

I'm studying kotlin, but I'm very disappointed, I can not compare two Strings.

What is the right way to compare.

btn_login.setOnClickListener {
            val login = input_email.text.trim()
            val pass = input_password.text.trim()

            if( login.equals( pass ) ){
                startActivity<MainActivity>()
            }

            if (login?.equals(other = pass)){
                startActivity<MainActivity>()
            }

            if (login == pass){
                startActivity<MainActivity>()
            }

        }

enter image description here

9 Answers

i know this is way too late, but as a newbie learning Kotlin, i had the same doubts.

then i came across this wonderful article that articulates the various string comparison types in Kotlin and the differences between them all.

in short both == and .equals() can be used to compare the value of 2 strings in kotlin.

hopefully that helps

KOTLIN:

if (editText1.text.toString() == editText2.text.toString() ) {
   println("Should work now! The same value")
}

Try this surely will work.

val style = buildString { karthik}
val style2 = buildString { karthik }
var result = style.equals(style2)
if(result){//Do something}
Related