Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare String Kotlin

Tags:

kotlin

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

like image 512
FlipNovid Avatar asked Jul 03 '17 01:07

FlipNovid


People also ask

Can you use == to compare two strings?

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.

How do you compare 2 strings?

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.

What's the difference between == and === operators in Kotlin?

Equality In Kotlin there are two types of equality: Structural equality ( == - a check for equals() ) Referential equality ( === - two references point to the same object)


1 Answers

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() 
like image 107
Dmitrii Nechepurenko Avatar answered Oct 27 '22 03:10

Dmitrii Nechepurenko