JavaScript 字符串 toLowerCase() 方法
JavaScript 字符串 toLowerCase() 方法将字符串中的所有字符转换为小写字母。它不会更改原始字符串,而是返回一个新字符串。
语法
以下是 JavaScript 字符串 toLowerCase() 方法的语法 -
toLowerCase()
参数
- 它不接受任何参数。
返回值
此方法返回一个所有字符均为小写的字符串。
示例 1
以下示例演示了此方法的用法。它将字符串"TUTORIALSPOINT"中的字符转换为小写字母。
<html>
<head>
<title>JavaScript String toLowerCase() Method</title>
</head>
<body>
<script>
const original_str = "TUTORIALSPOINT";
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>
输出
上述程序在输出中返回"tutorialspoint" -
Original string is: TUTORIALSPOINT New string(after converting into lowercase) is: tutorialspoint
示例 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
示例 3
我们使用 charAt() 和 toLowerCase() 方法将字符串中的特定字符转换为小写。在本例中,我们使用 charAt() 方法检索字符串中的特定字符,然后尝试使用 toLowerCase() 方法将其转换为小写。
<html>
<head>
<title>JavaScript String toLowerCase() Method</title>
</head>
<body>
<script>
const str = "TutorialsPoint";
document.write("String str = ", str);
document.write("<br>After converting a specific character '", str.charAt(9), "' into in lowercase: ");
document.write(str.charAt(9).toLowerCase());
</script>
</body>
</html>
输出
上述程序将特定字符"P"转换为小写。
String str = TutorialsPoint After converting a specific character 'P' into in lowercase: p

