Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uppercase first letter of only the first word [duplicate]

Tags:

javascript

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?

like image 256
user406151 Avatar asked Jul 13 '11 17:07

user406151


People also ask

How do you capitalize only the first letter of a sentence in Excel?

In cell B2, type =PROPER(A2), then press Enter. This formula converts the name in cell A2 from uppercase to proper case. To convert the text to lowercase, type =LOWER(A2) instead. Use =UPPER(A2) in cases where you need to convert text to uppercase, replacing A2 with the appropriate cell reference.

What does it mean when someone capitalizes the first letter of every word?

It's a traditional way of capitalizing titles, it's normally not to be used in articles (though nobody really prohibits it) - there Sentence case is the most widely used. In some regions it's considered more academic and thus could give a higher esteem to the writer.

How do I stop word capitalizing the first letter of a new sentence?

On the Tools menu, click AutoCorrect Options. Click the AutoCorrect tab. Click to clear the Capitalize first letter of sentences check box. Click OK.


2 Answers

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

like image 199
Karl Laurentius Roos Avatar answered Oct 04 '22 12:10

Karl Laurentius Roos


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(); })
like image 39
corbacho Avatar answered Oct 04 '22 12:10

corbacho