如何在 JavaScript 中转换字符串为小写
在本教程中,我们将学习如何在 JavaScript 中把一个字符串转换为小写。
JavaScript 使我们能够以多种方式转换字符串。在原生 JavaScript 中,我们可以使用 toLowerCase()
原型方法将字母小写,或者使用 toLocaleLowerCase()
原型,考虑到用户/宿主的语言环境来转换小写。
假设我们有一个字符串-My Awesome String
,我们想把它转换成小写的-my awesome string
。我们可以使用原型方法 toLowerCase()
来降低它的大小写。它从输入的字符串中创建一个新的字符串,但使用所有小写字符。
让我们看一个使用 console.log()
方法转换为小写并打印到控制台的例子。
var tempText = "My Awesome String";
console.log( tempText.toLowerCase() );
你也可以像下面的例子一样,在单引号后面添加原型。
var TextConvertedToLowerCase = 'My Awesome String'.toLowerCase();
console.log( TextConvertedToLowerCase );
如果字符串包含特定的本地映射,如土耳其语或德语,我们可以使用 toLocaleLowerCase()
方法将字符串转换为小写。
我们可以在不发送任何参数的情况下使用 toLocaleLowerCase()
,类似于 text.toLocaleLowerCase()
,也可以将本地化映射作为参数发送给该方法,我们甚至可以发送一个本地化数组,JavaScript 将选择最适合的本地化映射。
在下面的例子中,我们将把一个英文字符串转换为小写,并把一个土耳其拉丁文大写字母 I 转换为小写,以向你展示如何向函数发送一个 locale 数组。
var demoText = "My Awesome Second Demo Text";
console.log(demoText.toLocaleLowerCase());
console.log(demoText.toLocaleLowerCase('en-US'));
var localeArray = ['tr', 'TR'];
var LatinCapitalLetterIWithDotAboveInTurkish = '\u0130';
console.log(LatinCapitalLetterIWithDotAboveInTurkish.toLocaleLowerCase(localeArray));
输出:
my awesome second demo text
my awesome second demo text
i
如果我们对 JavaScript 不是那么熟悉,想把 toLowerCase()
的语法改成类似于我们喜欢的 Python 或 PHP 等语言的语法。在下面的例子中,我们将解释如何做到这一点。
function lower(inputString){
return String(inputString).toLowerCase();
}
var demoText = "Our Awesome String To Lower Converter";
console.log( lower(demoText) );
如果我们想把 Date
转换为小写,而 Date
本质上是一个非 String
对象,我们可以使用 toLowerCase()
或 toLocaleLowerCase()
方法,因为它们都实现了对任何值类型的通用工作。
让我们在下面的例子中看看如何将 Date()
转换为小写。
var normalDate = new Date();
var lowerCaseDate = new Date().toString().toLowerCase();
console.log("Normal Date Format > "+normalDate);
console.log("Lower Case Date Format > "+lowerCaseDate);
输出:
Normal Date Format > Thu Nov 12 2020 12:07:11 GMT+0000 (Coordinated Universal Time)
Lower Case Date Format > thu nov 12 2020 12:07:11 gmt+0000 (coordinated universal time)
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。