两个相同的 String 不会等于 true

Two identical String will not equal to true

我想比较来自两个不同列表的两个字符串对象,无论我是否使用 equals()contentEquals()==,始终为假。

是否与第一个列表的字符串如何放入其中有关?

编辑:看这张图片中的日志结果越来越奇怪了:

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

以上代码的输出日志:

编辑

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

您的示例格式错误,我认为缺少一些代码,但正如@snachmsm 指出的那样,contentEquals 是 Kotlin 中数组的一种方法。您应该使用 equals() 代替字符串(尽管 == 是 Kotlin 中的首选方式)

newWord 似乎没有被修剪。从您的日志中,它前面有一个额外的 space。

日志中这两行分别对应这段代码:

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

你可以看到你没有在第二行添加两个space但是你的黑色在

之前仍然有一个额外的space

您应该更正您的列表或执行 newWord.trim() 这将删除所有前导和尾随的白色spaces

否则你应该总是使用String.equals(otherString: String)s1 == s2(它们在kotlin中是一样的)