I've looked all over for how to capitalize the first character of every word of a string, but nothing helped me. I need to set an entered string to a heading capital character lower case one . I've tried this:
function titleCase(str) {
//converting the giving string into array
str =str.split(" ");
//iterating over all elem.s in the array
for(var i=0;i<str.length;i++){
//converting each elem. into string
str[i]=str[i].toString();
//converting the first char to upper case &concatenating to the rest chars
str[i]=str[i].toUpperCase(str[i].charAt(0))+ str[i].substring(1);
}
return str;
}
titleCase("I'm a little tea pot");
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
function capitalise(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
capitalise("smallletters") ;// Smallletters
You could simply do:
function capitalFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
Try something like this:
String.prototype.titleCase = function(){
return this[0].toUpperCase() + this.slice(1)
}
Usage:
"hello my name is Jacques".titleCase();
If you want to capitalize the character at the beginning of each word, try something like this:
String.prototype.capitalize = function(){
return this.split(" ")
.map(function(){
return this[0].toUpperCase() + this.slice(1);
}).join(" ");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With