2つの異なるリストから取得した2つのStringオブジェクトを比較したいのですがequals()
、contentEquals()
またはを使用しても、==
常にfalseです。
最初のリストの文字列がどのように配置されるかと関係がありますか?
編集:それはこの写真のログ結果を不思議に見ています:
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")}
}
newWord
トリミングされていないようです。ログから、その前に余分なスペースがあります。
ログのこれらの2行は、それぞれこのコードに対応しています。
Log.i("testen", "it is: $it and equals 'black'? - ${it.equals("black")}")
Log.i("testen", "it is: $it and equals $newWord - ${it.equals(newWord)}")
2行目に2つのスペースを追加していませんが、黒の前にはまだスペースがあります。
リストを修正するかnewWord.trim()
、先頭と末尾の空白をすべて削除することを行う必要があります
それ以外の場合は、常にString.equals(otherString: String)
or を使用する必要がありますs1 == s2
(kotlinでも同じです)。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加