js将字母全部大写
要将一个字符串中的所有字母转换为大写,可以使用JavaScript的toUpperCase()方法。
以下是将字符串全部转换为大写的示例代码:
let str = "hello world";
let upperCaseStr = str.toUpperCase();
console.log(upperCaseStr); // 输出 "HELLO WORLD"
在上述示例中,首先定义一个字符串str,然后使用toUpperCase()方法将其转换为大写,并将结果赋值给upperCaseStr变量。最后,使用console.log()方法将结果打印到控制台上。
请注意,toUpperCase()方法不会改变原始字符串,而是返回一个新的字符串,其中所有字母都是大写的。如果要改变原始字符串,请将结果重新赋值给原始变量,如下所示:
let str = "hello world";
str = str.toUpperCase();
console.log(str); // 输出 "HELLO WORLD"
这样,字符串str的值将被更新为大写形式
原文地址: http://www.cveoy.top/t/topic/iJO0 著作权归作者所有。请勿转载和采集!