gpt4 book ai didi

android - 两个相同的 String 将不等于 true

转载 作者:行者123 更新时间:2023-12-02 12:44:39 25 4
gpt4 key购买 nike

我想比较来自两个不同列表的两个 String 对象,无论我是否使用equals() , contentEquals()== ,总是假的。
与第一个列表的字符串如何放入其中有关吗?
编辑:看这张图片中的日志结果越来越奇怪:
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
}})
上述代码的输出日志:
Outcome of the logging
编辑
 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")}
}

最佳答案

newWord似乎没有修剪。从您的日志中,它前面有一个额外的空间。
enter image description here
日志中这两行分别对应这段代码:

Log.i("testen", "it is: $it and equals 'black'? - ${it.equals("black")}")
Log.i("testen", "it is: $it and equals $newWord - ${it.equals(newWord)}")
你可以看到你没有在第二行添加两个空格,但你的黑色之前仍然有一个额外的空格

您应该更正您的列表或执行 newWord.trim()这将删除所有前导和尾随空格
否则你应该总是使用 String.equals(otherString: String)s1 == s2 (它们在 kotlin 中是一样的)

关于android - 两个相同的 String 将不等于 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63628710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com