可以使用 for 循环来比较两个不同字符串的字符,具体步骤如下:
以下是示例代码:
function compareStrings(str1, str2) {
var len1 = str1.length;
var len2 = str2.length;
if (len1 !== len2) {
return false;
}
for (var i = 0; i < len1; i++) {
if (str1.charCodeAt(i) !== str2.charCodeAt(i)) {
return false;
}
}
return true;
}
// 测试
console.log(compareStrings("hello", "hello")); // true
console.log(compareStrings("hello", "world")); // false