Uppercase first letter of only the first word

Possible Duplicate:
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(); })
链接地址: http://www.djcxy.com/p/17860.html

上一篇: 我怎样才能让变量的首字母总是大写?

下一篇: 只有第一个单词的大写首字母