Javascript 在Angular-6中比较两个字符串的问题
我试图创建一个if语句来比较两个字符串,但出于同样的原因,有些东西不起作用Javascript 在Angular-6中比较两个字符串的问题,javascript,angular,Javascript,Angular,我试图创建一个if语句来比较两个字符串,但出于同样的原因,有些东西不起作用 console.log(this.selectedDay.split("/")[2]) console.log(this.selectedMonth) if( this.selectedMonth !== this.selectedDay.split("/")[2]) { console.log("The strings are different")
console.log(this.selectedDay.split("/")[2])
console.log(this.selectedMonth)
if( this.selectedMonth !== this.selectedDay.split("/")[2]) {
console.log("The strings are different")
} else {
console.log("The strings are the same")
}
[编辑]
当我手动比较这两个值时,结果是正确的
if( 'נובמבר 2018' !== 'נובמבר 2018') {
console.log("The strings are different")
} else {
console.log("The strings are the same")
}
[编辑二]
将拆分结果分配给变量
const value1 = this.selectedMonth;
const value2 = this.selectedDay.split("/")[2];
console.log(value1)
console.log(value2)
if( value1 !== value2) {
console.log("The strings are different")
} else {
console.log("The strings are the same")
}
})
[编辑部3]
我像用户@ConnorsFan建议的那样将所有字符串转换为数组,这就是出现的结果。疯狂
const value1 = this.selectedMonth;
const value2 = this.selectedDay.split("/")[2];
console.log(value1);
console.log(value2);
console.log(value1.split(""));
console.log(value2.split(""));
if( value1.valueOf() === value2.valueOf()) {
console.log("The strings are the same")
} else {
console.log("The strings are diffrent")
}
})
您可能是误用了空格吗?@GiladBar没有,我仔细检查了一下。您能为这个添加值吗?selectedDay?试试
console.log(value1.split(“”)代码>(与value2
相同)。它将输出单个字符,可能会显示一些意想不到的信息。您甚至可以尝试console.log(value1.split(“”).map(x=>x.charCodeAt(0))
以查看字符代码。该字符代码符合“意外”条件:-)