开发者

Uppercase first letter of only the first word [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplic开发者_如何转开发ate:

Capitalize the first letter of string in JavaScript

How do you force the first letter of the first word in a field to uppercase?


Asked before: How do I make the first letter of a string uppercase in JavaScript?

The correct answer:

function capitalizeFirstLetter(string)
{
    return string.charAt(0).toUpperCase() + string.slice(1);
}

You can use it like this:

var myString = "hello world";
alert(capitalizeFirstLetter(myString));

And it would alert Hello world


You don't need JavaScript, it's enough with the CSS :first-letter pseudo-element.

If you want do it via JavaScript, it has been asked before:

str.replace(/^\w/, function($0) { return $0.toUpperCase(); })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜