javascript - 对于控制台中的相等字符串,javascript 字符串比较不返回 true [已解决]
问题描述
我正在处理单词和它们的音素。我发现在我的代码(和控制台)中,例如两个相同的字符串" 'b eh1 r z'"
在比较时不会返回 true,无论是双等还是三等。我在 chrome 控制台、节点控制台和文件本身中进行了健全性测试,它们都返回了预期的结果(即只有 'strinfigied' 变量似乎已损坏。我绞尽脑汁想弄清楚发生了什么。这个是什么不符合预期:
let stringified = trialPhonemeSequence.join(" ")
if (p == "z"){
console.log(trialPhonemeSequence)
let bearstring = 'b eh1 r z'
console.log("SHould be adding 'z' at ", i, "so we got", trialPhonemeSequence, "and stringified", stringified)
console.log(`String|${dictionary['bears']}| length ${dictionary['bears'].length} should equal |${stringified}| length ${stringified.length}: ${dictionary['bears'] == stringified} and ${bearstring == stringified}`);
}
Chrome 控制台输出的内容
String|b eh1 r z| length 10 should equal |b eh1 r z| length 10: false and false
这是到那个时候的整个函数的上下文。我认为您不需要整个最小可重复代码,因为它需要大型字典和数据集以及初始化。此函数的目标是输入 Bear 并查找音位匹配的单词,允许添加音位(本测试用例中的“z”音)。
function findAddedPhonemes(word, dictionary, hashMap)
{
let matches = []
let phonemeSequence = dictionary[word]
let phonemeSequenceList = phonemeSequence.split(" ")
for (let i = 0; i <= phonemeSequenceList.length; i++)
{
phonemeList.forEach((p, ind) => // all the items in the list
{
let trialPhonemeSequence = phonemeSequenceList.slice()
trialPhonemeSequence.splice(i, 0, p) // insert p phoneme into index
let stringified = trialPhonemeSequence.join(" ")
if (p == "z"){
console.log(trialPhonemeSequence)
let bearstring = 'b eh1 r z'
console.log(`String|${dictionary['bears']}| length ${dictionary['bears'].length} should equal |${stringified}| length ${stringified.length}: ${dictionary['bears'] == stringified} and ${bearstring == stringified}`);
}
if (stringified == "b eh1 r z"){ //THIS IS WHERE ITS BROKEN
console.log("Bears stringified searching!!!!!!!!!!!!")
}
let hash = stringified.hashCode(dictKeys.length * 4)
if (hashMap[hash] !== undefined)
{
hashMap[hash].forEach((o) =>
{
let key = getObjectsKey(o)
if (checkIfIdentical(dictionary[key], stringified))
{
matches.push(key)
}
})
}
})
}
console.log("Matches", matches)
return matches
}
编辑(已解决):
字符串化字符串中有一个 char 13(回车),但其他字符串没有。我想我明白这是从哪里来的。我在单词的每个音节中插入了一个带有拼接的新音素,当将它拼接到单词的末尾时,它不会自动剥离“\ n”,这会导致比较错误。我现在知道必须手动执行此操作并且哈希值错误。顺便说一句,音素字典在这里
谢谢@VLAZ!
stringified.split("").map(c => {
console.log(c.charCodeAt(0))
})
console.log("New word")
bearstring.split("").map(c => {
console.log(c.charCodeAt(0))
})
console.log(stringified==bearstring)
解决方案
推荐阅读
- sql - SQL Server:执行更新查询时,无法执行删除查询
- ios - 如何获得正确的夏令时缩写
- python - 为什么每次迭代后整列的值都会发生变化,而不仅仅是一个特定的单元格?
- sql - 使用 Spring JPA 检查集合中的完全匹配
- python - Tkinter 可以“卸载”自己吗?
- python - Pygame没有检测到鼠标点击
- c# - 是否可以通过使用 Task.Delay 强制一个新线程来确定任务的并行性?
- android - Firebase 未检测到我的颤振应用程序,并且无法发送推送通知
- java - 强制对象字段仅具有从父类继承的某些值
- r - 在 ggplot 地图中表示不同的图层!R