JavaScript 中的 toLowerCase() 和 toUpperCase 字符串方法

toLowerCase() 和 toUpperCase() 在一个字符串上返回相同的字符串,并将案例转换为下级或上级案例:

1var inconsistent = Up AnD dOwn;
2
3console.log(inconsistent.toLowerCase());
4
5// “up and down”

语法

1// toLowerCase()
2myString.toLowerCase();
3
4// toUpperCase
5myString.toUpperCase();

注意事项 使用

这在比较字符串时最有用,因为你可以忽略案例不一致并专注于实际单词,但有时你需要检查案例,但这已经涵盖了你没有的情况:

1var typeface = "Sans-serif";
2
3console.log(typeface.toLowerCase() == "sans-serif");
4
5// true
Published At
Categories with 技术
Tagged with
comments powered by Disqus