Javascript 如何使字符串的第一个字母大写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5886936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:22:19  来源:igfitidea点击:

How do I make the first letter of a string uppercase?

javascript

提问by 0x499602D2

Possible Duplicate:
Capitalize first letter of string in javascript

可能的重复:
在 javascript 中将字符串的第一个字母大写

How do I make the first letter of a string uppercase?

如何使字符串的第一个字母大写?

回答by meouw

Here's a function that does it

这是一个函数

function firstToUpperCase( str ) {
    return str.substr(0, 1).toUpperCase() + str.substr(1);
}

var str = 'hello, I\'m a string';
var uc_str = firstToUpperCase( str );

console.log( uc_str ); //Hello, I'm a string