42

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

glee8e
  • 6,180
  • 4
  • 31
  • 51
FlipNovid
  • 1,191
  • 3
  • 12
  • 20

9 Answers9

48

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()
Dmitrii Nechepurenko
  • 1,414
  • 1
  • 11
  • 13
14

Here is the example for matching the two strings using kotlin.

If you are using == (double equals) for matching the string then it's compare the address & return maximum time wrong result as per java documentation so use equals for the same

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

if (s1.equals(s2,true))

other wise you can just use this without boolean like

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

compleate code is below

 fun main(args: Array<String>) {
 val s1 = "abc"
 val s2 = "Abc"
 if (s1.equals(s2,true))
 {
    println("Equal")
 }
 else
 {
    println("Not Equal")
 }
}
Maraj Hussain
  • 1,580
  • 10
  • 26
  • 1
    why "try" ?? A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. Please add some description to make others understand. :) – Rucha Bhatt Joshi Sep 05 '17 at 07:00
  • 2
    I have updated the answered with some explanation & thanks for the appreciate me. :) – Maraj Hussain Sep 05 '17 at 07:49
  • 8
    Your answer is incorrect. Using `==` operator to compare strings is translated to `.equals()` call automatically as the operator is checking *structural equality* (https://kotlinlang.org/docs/reference/equality.html#structural-equality) – Matej Kormuth Feb 15 '19 at 11:56
9

Covert both the SpannableStringBuilder to string with toString, this should work.

val login = input_email.text.trim().toString()
val pass = input_password.text.trim().toString()
if (login == pass){
    startActivity<MainActivity>()
}
koiralo
  • 22,594
  • 6
  • 51
  • 72
5

1. == :

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

2. equals :

Indicates whether some other object is "equal to" this one. Implementations must fulfil the following requirements: Reflexive: for any non-null reference value x, x.equals(x) should return true.

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

Transitive: for any non-null 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

Consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.

/**  * 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 object for order. Returns zero if this object is equal to the specified other object, a negative number if it's less than other, or a positive number if it's greater than other.

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

Compares two strings lexicographically, optionally ignoring case differences

Misagh
  • 3,403
  • 1
  • 20
  • 17
5

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

vivek86
  • 707
  • 9
  • 18
4

With case checking

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

IgnoreCase

if(a.equals(b,false))
Santhosh
  • 160
  • 7
1

KOTLIN:

if (editText1.text.toString() == editText2.text.toString() ) {
   println("Should work now! The same value")
}
J A S K I E R
  • 1,976
  • 3
  • 24
  • 42
0

Try the following solution, see if it helps:

val passStr: String = textView.text.toString()  
if( loginStr.compareTo(passStr, false) ){
            startActivity<MainActivity>()
        }
Fabien
  • 4,862
  • 2
  • 19
  • 33
lannyf
  • 9,865
  • 12
  • 70
  • 152
0

Try this surely will work.

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