如何将JavaScript字符串值转换为小写字母?

例子:从“Your Name”到“Your Name”


当前回答

var lowerCaseName = "Your Name".toLowerCase();

其他回答

toLocaleUpperCase()或小写函数不像它们应该做的那样。例如,在我的系统上,使用Safari 4、Chrome 4 Beta和Firefox 3.5。x,它会错误地转换带有土耳其字符的字符串。浏览器响应导航器。分别为“en-US”、“tr”、“en-US”。

但据我所知,没有任何方法可以在浏览器中获得用户的Accept-Lang设置。

只有Chrome给我带来了麻烦,尽管我已经将每个浏览器配置为首选的tr-TR语言环境。我认为这些设置只影响HTTP报头,但我们不能通过JavaScript访问这些设置。

在Mozilla文档中,它说“字符串中的字符被转换为……”同时尊重当前区域设置。对于大多数语言,这将返回相同的…”。我认为它对土耳其语是有效的,如果配置为en或tr,它没有区别。

在土耳其语中,它应该将“DİNÇ”转换为“dinç”,将“DINÇ”转换为“dınç”,反之亦然。

var lowerCaseName = "Your Name".toLowerCase();

简单地使用JS toLowerCase() let v = "Your Name" let u = v.toLowerCase();或 let u = "Your Name".toLowerCase();

const str = 'Your Name';

// convert string to lowercase
const lowerStr = str.toLowerCase();

// print the new string
console.log(lowerStr);

如果你想自己构建:

function toLowerCase(string) {

    let lowerCaseString = "";

    for (let i = 0; i < string.length; i++) {
        // Find ASCII charcode
        let charcode = string.charCodeAt(i);

        // If uppercase
        if (charcode > 64 && charcode < 97) {
            // Convert to lowercase
            charcode = charcode + 32
        }

        // Back to char
        let lowercase = String.fromCharCode(charcode);

        // Append
        lowerCaseString = lowerCaseString.concat(lowercase);
    }

    return lowerCaseString
}