Compare String Kotlin

I am learning kotlin, but I am very disappointed, I can not compare two lines.

What is the correct 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

+7
kotlin
source share
6 answers

According to the documentation for using structural equality == . It is translated into a?.equals(b) ?: (b === null) .

In your case, convert the login and go from SpannableStringBuilder to String.

  val login = input_email.text.trim().toString() 
+12
source share

Hide SpannableStringBuilder to string using toString , this should work.

 val login = input_email.text.trim().toString() val pass = input_password.text.trim().toString() if (login == pass){ startActivity<MainActivity>() } 
+5
source share

Here is an example for matching two strings using kotlin.

If you use == (double equals) to match the string, then it compares the address and returns the maximum time of the incorrect result according to the java documentation, so use equals for the same

If you want to use the equal case of ignoring , then pass true in the equals String method

 if (s1.equals(s2,true)) 

another wise you can just use this without a boolean type

 if (s1.equals(s2,false)) or if (s1.equals(s2)) 

completion code below

  fun main(args: Array<String>) { val s1 = "abc" val s2 = "Abc" if (s1.equals(s2,true)) { println("Equal") } else { println("Not Equal") } } 
+4
source share

1. == :

if ( string1 == string2 ){...}

2. equals :

Indicates whether any other object is "equal" to this. Implementations must meet the following requirements: Return: for any non-zero reference value x, x.equals (x) should return true.

Symmetric: for any non-empty reference values โ€‹โ€‹x and y, x.equals (y) should return true if and only if y.equals (x) returns true.

Transitive: for any non-zero reference values โ€‹โ€‹x, y and z, if x.equals (y) returns true and y.equals (z) returns true, then x.equals (z) should return true

Consistency: for any non-empty reference values โ€‹โ€‹x and y, several invocations of x.equals (y) successively return true or successively return false if any information used in equal comparisons is changed to objects.

 /** * Returns `true` if this string is equal to [other], optionally ignoring character case. * * @param ignoreCase `true` to ignore character case when comparing strings. By default `false`. */ public fun String?.equals(other: String?, ignoreCase: Boolean = false): Boolean 

3. compareTo :

public override fun compareTo(other: String): Int

Compares this object with the specified order object. Returns zero if this object is equal to the specified other object, a negative number if it is less than another, or a positive number if it is more than others.

 public fun String.compareTo(other: String, ignoreCase: Boolean = false): Int 

Compares two lines lexicographically, not necessarily ignoring the case of differences

+2
source share

With case verification

 String a=..... String b=..... if(a==b){ } 

Ignorecase

 if(a.equals(b,false)) 
0
source share

Try the following solution, see if it helps:

 val passStr: String = textView.text.toString() if( loginStr.compareTo(passStr, false) ){ startActivity<MainActivity>() } 
0
source share

All Articles