gpt4 book ai didi

javascript toLowerCase() 函数返回不同的字符串

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:42:24 24 4
gpt4 key购买 nike

console.log("HİNDİ".toLocaleLowerCase() == "hindi");
console.log("HİNDİ" == "hindi");

console.log("HİNDİ".toLowerCase());


console.log("HİNDİ".toLocaleLowerCase())
console.log("HİNDİ".toLowerCase())

我正在构建一个搜索功能,但我遇到了一个问题:

"HİNDİ".toLocaleLowerCase() // "hindi"

"hindi" == "HİNDİ".toLocaleLowerCase() //false

这到底是怎么回事?

解决方案:@pmrotule 的回答似乎有效:

function to_lower(s)
{
var n = "";
for (var i = 0; i < s.length; i++) // do it for one character at a time
{
var c = s[i].toLowerCase();

// call replace() only if the character has a length > 1
// after toLowerCase()
n += c.length > 1 ? c[0].replace(/[^ -~]/g,'') : c;
}
return n;
}

谢谢,

最佳答案

是字符串格式的问题。 toLocaleLowerCase 仅用于人类可读的显示。但是,您仍然可以使用一个技巧:

if ("hindi" == "HİNDİ".toLowerCase().replace(/[^ -~]/g,''))
{
alert("It works!");
}

编辑

如果你想让它适用于所有特殊字符:

function to_lower(s)
{
var n = "";
for (var i = 0; i < s.length; i++) // do it for one character at a time
{
var c = s[i].toLowerCase();

// call replace() only if the character has a length > 1
// after toLowerCase()
n += c.length > 1 ? c.replace(/[^ -~]/g,'') : c;
}
return n;
}

console.log("gök" == to_lower("GÖK"));
console.log("hindi" == to_lower("HİNDİ"));

function to_low(s) // shorter version
{
var n = "";
for (var i = 0; i < s.length; i++)
{ n += s[i].toLowerCase()[0]; }

return n;
}

console.log("hindi" == to_low("HİNDİ"));

关于javascript toLowerCase() 函数返回不同的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37791201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com