JavaScript String toLowerCase() 方法将字符串中的所有字符转换为小写字母。它不会更改原始字符串,但会返回一个新字符串。
语法
以下是 JavaScript String toLowerCase() 方法的语法 -
toLowerCase()
参数
它不接受任何参数。
返回值
此方法返回一个字符串,其中所有字符均为小写。
示例 1
以下示例演示了如何使用此方法。它将字符串 “QIKEPUCOM” 的字符转换为小写字母。
<html> <head> <title>JavaScript String toLowerCase() Method</title> </head> <body> <script> const original_str = "
QIKEPUCOM"; document.write("Original string is: ", original_str); const lowercase_str = original_str.toLowerCase(); document.write("<br>New string(after converting into lowercase) is: ", lowercase_str); </script> </body> </html>
输出
上面的程序在输出中返回 “qikepucom” -
Original string is: QIKEPUCOM
New string(after converting into lowercase) is: qikepucom
New string(after converting into lowercase) is: qikepucom
示例 2
将其中一个字符串 “HELLOWORLD” 转换为小写字母后,让我们比较两个字符串,并根据满足的条件显示一个语句。
<html>
<head>
<title>JavaScript String toLowerCase() Method</title>
</head>
<body>
<script>
const str1 = "helloworld";
const str2 = "HELLOWORLD";
document.write("str1 = ", str1);
document.write("<br>str2 = ", str2);
document.write("<br>Before conversion:<br>");
if(str1 === str2){
document.write("String str1 and str2 are equal");
}
else{
document.write("String str1 and str2 are not equal");
}
document.write("<br>After conversion:<br>");
if(str1 === str2.toLowerCase()){
document.write("String str1 and str2 are equal");
}
else{
document.write("String str1 and str2 are not equal");
}
</script>
</body>
</html>
输出
执行上述程序后,它显示以下语句为 -
str1 = helloworld
str2 = HELLOWORLD
Before conversion:
String str1 and str2 are not equal
After conversion:
String str1 and str2 are equal
str2 = HELLOWORLD
Before conversion:
String str1 and str2 are not equal
After conversion:
String str1 and str2 are equal
示例 3
让我们使用 charAt() 和 toLowerCase() 方法将字符串中的特定字符转换为小写。在此示例中,我们使用 charAt() 方法检索字符串中的特定字符,并尝试使用 toLowerCase() 方法将其转换为小写。
<html>
<head>
<title>JavaScript String toLowerCase() Method</title>
</head>
<body>
<script>
const str = "QikepuCom";
document.write("String str = ", str);
document.write("<br>After converting a specific character '", str.charAt(6), "' into in lowercase: ");
document.write(str.charAt(6).toLowerCase());
</script>
</body>
</html>
输出
上面的程序将特定字符 'C' 转换为小写。
String str = QikepuCom
After converting a specific character 'C' into in lowercase: c
After converting a specific character 'C' into in lowercase: c