Javascript 字符串和变量的非质量
Nonequality for Javascript String and Variable
我正在尝试使用 .includes()
Javascript 方法检查名为 ruleFolderNames
的数组是否包含字符串。我正在检查的字符串是 "Centers For Medicare & Medicaid Services"
.
ruleFolderNames.includes("Centers For Medicare & Medicaid Services"); // true
但是,我没有直接输入字符串,而是使用了一个变量(称为 department
)。我发现 department
变量 不 等于数组中的第一项,Centers For Medicare & Medicaid Services
、,尽管它们看起来是同样。
这里是 console.log
语句:
ruleFolderNames[0]; // Centers For Medicare & Medicaid Services
typeof ruleFolderNames[0]; // string
department; // Centers For Medicare & Medicaid Services
typeof department; // string
但实际上变量department
并不等于我要查找的字符串
department == "Centers For Medicare & Medicaid Services" // OUTPUTS FALSE!!!
我已将 link 添加到我的完整 Github repository if that's useful, the bug is contained on line 36 of this 文件中。
由于我们自己没有实际数据可以调试,所以无法准确告诉您有什么不同。以下是一些需要寻找的东西:
检查两个字符串的长度,看看前导或尾随白色是否存在差异space,这在控制台中并不明显。您还可以在控制台中输出并在其周围加上引号,以准确查看开始或结束的内容,例如 console.log(`'${department}'`)
和 ruleFolderNames.forEach(item => console.log(`'${item}'`))
.
白色 space 字符可能不同,但在控制台中看起来像普通的白色space。如果您怀疑差异在于部门变量(看起来确实如此),那么您可以使用 department.replace(/\s/g, " ")
将 whitespace 标准化,这会将所有正则表达式 whitespace 字符转换为单个 space.
可能有些字符看起来很相似,但实际上是不同的字符代码。
既然你说 department == "Centers For Medicare & Medicaid Services"
是错误的,我建议使用以下调试代码(将你的 department
变量传递给 testStr()
:
function testStr(testVal, targetStr) {
console.log("-------------");
if (testVal === targetStr) {
console.log("two strings are the same");
return;
}
if (testVal.length !== targetStr.length) {
console.log("two strings are different length");
if (testVal.trim() === targetStr) {
console.log("Found it: testVal.trim() === targetStr");
return;
} else {
console.log("testVal.trim() !== targetStr, trimming whitespace off ends is not enough");
}
testVal = testVal.trim();
}
if (testVal.replace(/\s/g, " ") === targetStr) {
console.log("replacing whitespace makes a match")
return;
}
// still no match, so lets output each separate character as a char code
// to manually see what's different
console.log("testVal = ", testVal.split("").map(item => item.charCodeAt(0)).join(","));
console.log("targetStr = ", targetStr.split("").map(item => item.charCodeAt(0)).join(","));
}
const testValues = [
" Centers For Medicare & Medicaid Services", // leading whitespace
"Centers For Medicare & Medicaid Services ", // trailing whitespace
"Centers\tFor Medicare & Medicaid Services", // tab for whitespace
"C℮nters For Medicare & Medicaid Services", // odd first e in Centers
"Cënters For Medicare & Medicaid Services" // odd first e in Centers
];
for (let t of testValues) {
testStr(t, "Centers For Medicare & Medicaid Services");
}
我正在尝试使用 .includes()
Javascript 方法检查名为 ruleFolderNames
的数组是否包含字符串。我正在检查的字符串是 "Centers For Medicare & Medicaid Services"
.
ruleFolderNames.includes("Centers For Medicare & Medicaid Services"); // true
但是,我没有直接输入字符串,而是使用了一个变量(称为 department
)。我发现 department
变量 不 等于数组中的第一项,Centers For Medicare & Medicaid Services
、,尽管它们看起来是同样。
这里是 console.log
语句:
ruleFolderNames[0]; // Centers For Medicare & Medicaid Services
typeof ruleFolderNames[0]; // string
department; // Centers For Medicare & Medicaid Services
typeof department; // string
但实际上变量department
并不等于我要查找的字符串
department == "Centers For Medicare & Medicaid Services" // OUTPUTS FALSE!!!
我已将 link 添加到我的完整 Github repository if that's useful, the bug is contained on line 36 of this 文件中。
由于我们自己没有实际数据可以调试,所以无法准确告诉您有什么不同。以下是一些需要寻找的东西:
检查两个字符串的长度,看看前导或尾随白色是否存在差异space,这在控制台中并不明显。您还可以在控制台中输出并在其周围加上引号,以准确查看开始或结束的内容,例如
console.log(`'${department}'`)
和ruleFolderNames.forEach(item => console.log(`'${item}'`))
.白色 space 字符可能不同,但在控制台中看起来像普通的白色space。如果您怀疑差异在于部门变量(看起来确实如此),那么您可以使用
department.replace(/\s/g, " ")
将 whitespace 标准化,这会将所有正则表达式 whitespace 字符转换为单个 space.可能有些字符看起来很相似,但实际上是不同的字符代码。
既然你说 department == "Centers For Medicare & Medicaid Services"
是错误的,我建议使用以下调试代码(将你的 department
变量传递给 testStr()
:
function testStr(testVal, targetStr) {
console.log("-------------");
if (testVal === targetStr) {
console.log("two strings are the same");
return;
}
if (testVal.length !== targetStr.length) {
console.log("two strings are different length");
if (testVal.trim() === targetStr) {
console.log("Found it: testVal.trim() === targetStr");
return;
} else {
console.log("testVal.trim() !== targetStr, trimming whitespace off ends is not enough");
}
testVal = testVal.trim();
}
if (testVal.replace(/\s/g, " ") === targetStr) {
console.log("replacing whitespace makes a match")
return;
}
// still no match, so lets output each separate character as a char code
// to manually see what's different
console.log("testVal = ", testVal.split("").map(item => item.charCodeAt(0)).join(","));
console.log("targetStr = ", targetStr.split("").map(item => item.charCodeAt(0)).join(","));
}
const testValues = [
" Centers For Medicare & Medicaid Services", // leading whitespace
"Centers For Medicare & Medicaid Services ", // trailing whitespace
"Centers\tFor Medicare & Medicaid Services", // tab for whitespace
"C℮nters For Medicare & Medicaid Services", // odd first e in Centers
"Cënters For Medicare & Medicaid Services" // odd first e in Centers
];
for (let t of testValues) {
testStr(t, "Centers For Medicare & Medicaid Services");
}