I\'m trying to proper case a string in javascript - so far I have this code: This doesn\'t seem to capitalize the first letter, and I\'m also stuck on how to lowercase all the l
Here's a function titleCase(string, array)
that transforms a string into title case, where the first letter of every word is capitalized except for certain prepositions, articles and conjunctions. If a word follows a colon, it will always be capitalized. You can include an optional array to ignore strings of your choice, such as acronyms.
I may have missed some exception words in the ignore
array. Feel free to add them.
function titleCase(str, array){
var arr = [];
var ignore = ["a", "an", "and", "as", "at", "but", "by", "for", "from", "if", "in", "nor", "on", "of", "off", "or", "out", "over", "the", "to", "vs"];
if (array) ignore = ignore.concat(array);
ignore.forEach(function(d){
ignore.push(sentenceCase(d));
});
var b = str.split(" ");
return b.forEach(function(d, i){
arr.push(ignore.indexOf(d) == -1 || b[i-1].endsWith(":") ? sentenceCase(d) : array.indexOf(d) != -1 ? d : d.toLowerCase());
}), arr.join(" ");
function sentenceCase(x){
return x.toString().charAt(0).toUpperCase() + x.slice(x.length-(x.length-1));
}
}
var x = titleCase("james comey to remain on as FBI director", ["FBI"]);
console.log(x); // James Comey to Remain on as FBI Director
var y = titleCase("maintaining substance data: an example");
console.log(y); // Maintaining Substance Data: An Example
I recently redid this problem using regex which matches the first letter and accounts for apostrophe. Hope it's helpful:
function titleCase(str) {
return str.toLowerCase().replace(/^\w|\s\w/g, function(firstLetter) {
return firstLetter.toUpperCase();
});
}
titleCase("I'm a little tea pot");
This should work. Notice how I set newstr[i]
to the desired output. Functions like .toUpperCase()
do not affect the original string. They only return a new string with the desired property.
function titleCase(str) {
var newstr = str.split(" ");
for(i=0;i<newstr.length;i++){
if(newstr[i] == "") continue;
var copy = newstr[i].substring(1).toLowerCase();
newstr[i] = newstr[i][0].toUpperCase() + copy;
}
newstr = newstr.join(" ");
return newstr;
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
This function uppercases 1st letter and lowercases the rest part od the string.
A bit changed function from the perfect answer found here: How do I make the first letter of a string uppercase in JavaScript?
const titleCase = str => {
let string = str.toLowerCase().split(" ");
let arr = [];
string.map(x =>arr.push(x[0].toUpperCase() + x.slice(1)))
return arr.join(" ");
}
If you enjoy using Ramda like I do, you can do this clean fun thing:
import { concat, compose, head, join, map, split, tail, toLower, toUpper } from 'ramda';
const toWords = split(' ');
const capitalizeWords = map(s => concat(toUpper(head(s)), toLower(tail(s))));
const toSentence = join(' ');
const toTitleCase = compose(toSentence, capitalizeWords, toWords);
or of course you can always cut it down to
const capitalizeWords = map(s => concat(toUpper(head(s)), toLower(tail(s))));
const toTitleCase = compose(join(' '), capitalizeWords, split(' '));