Compare String Kotlin

2019-06-14 21:54发布

问题:

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>()
            }

        }

回答1:

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()


回答2:

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")
 }
}


回答3:

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>()
}


回答4:

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



回答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



回答6:

With case checking

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

IgnoreCase

if(a.equals(b,false))


回答7:

Try the following solution, see if it helps:

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


回答8:

Try this surely will work.

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


标签: kotlin