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>() } }
You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.
Equality In Kotlin there are two types of equality: Structural equality ( == - a check for equals() ) Referential equality ( === - two references point to the same object)
According to documentation for structual equality use ==
. It is translated to a?.equals(b) ?: (b === null)
.
In you case convert login and pass from SpannableStringBuilder to String.
val login = input_email.text.trim().toString()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With