Two identical String will not equal to true

Viewed 118

I want to compare two String objects coming out of two different lists and no matter if i use equals(), contentEquals() or ==, it is always false.

Has it something to do with how the strings of the first list are put into it?

edit: it's getting weirder look at the log outcome in this picture:

enter image description here

                        DictWord.dictWords.forEach {
                            Log.i("testen", "it is: $it and equals 'black'? - ${it.equals("black")}")
                            Log.i("testen", "it is: $it and equals $newWord - ${it.equals(newWord)}")
                            Log.i("testen", "it is: $it and equals $newWord - ${it.contentEquals("black")}")
                            Log.i("testen", "it is: $it and == $newWord - ${it == newWord}")
                            Log.i("black", "it is: 'black' and equals $newWord - ${"black" == newWord}")


...    subStrainsAdapter.addHeaderAndSubmitList(null)
            var textList = mutableListOf<String>()
            var movingText = ""
            thoughtContent.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable) {}
                override fun beforeTextChanged(
                    s: CharSequence,
                    start: Int, count: Int, after: Int) {}
                override fun onTextChanged(
                    s: CharSequence,
                    start: Int, before: Int, count: Int)
                {movingText += s.last()}
            })

            //SUBSTRAIN INPUT - goes to onSubStrainEditEnd above when ENTER hit
            thoughtContent.setOnKeyListener(object : View.OnKeyListener {
                @RequiresApi(Build.VERSION_CODES.Q)
                override fun onKey(v: View?, key: Int, event: KeyEvent): Boolean {
                    return if (event.action == KeyEvent.ACTION_DOWN && key == KeyEvent.KEYCODE_SPACE) {
                        textList.add(movingText)
                        movingText = ""
                        false } else false
                }})

Output log for the above code: Outcome of the logging

edit

 if(b == false) {
                    thoughtsViewModel.editThought(thoughtContent.text.toString(), thoughtItem.id)
                    val testList = thoughtContent.text.toString().split(" ")
                    textList.forEach {
                        (Log.i("testen", "it is $it"))
                        if(DictWord.dictWords.keys.contains(it)) {Log.i("testen", "TRIGGGEERRRED and its $it")}
                    }
                    testList.forEach {
                        (Log.i("testen", "it is $it"))
                        if(DictWord.dictWords.keys.contains(it)) {Log.i("testen", "test list TRIGGGEERRRED and its $it")}
                    }
2 Answers

newWord is not trimmed it seems. From your log, it has an extra space before it.

enter image description here

These two lines in the log correspond to this code respectively:

Log.i("testen", "it is: $it and equals 'black'? - ${it.equals("black")}")
Log.i("testen", "it is: $it and equals $newWord - ${it.equals(newWord)}")

You can see that you didn't add two spaces in the second line but still your black has an extra space before


You should either correct your list or do newWord.trim() which will remove all leading and trailing whitespaces

Otherwise you should always use String.equals(otherString: String) or s1 == s2 (they are the same in kotlin)

Your example is badly formatted and I believe missing some code, but as @snachmsm pointed out, contentEquals is a method on arrays in Kotlin. You should use equals() for Strings instead (though == is the preferred way in Kotlin)

Related