21

I.e., if I have an input string:

input = 'hello World, whatS up?'

I want an output string to be:

desiredOutput = 'Hello World, whats up?'

If the first letter of any word in the string is already in upper case, leave it as is.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jasan
  • 11,475
  • 22
  • 57
  • 97
  • Do you really want "whats up" or rather "Whats Up"? – Christian Zosel Oct 22 '16 at 18:48
  • Please read this http://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript – ivana helvin Oct 22 '16 at 18:49
  • 1
    input.charAt(0).toUpperCase() + input.toLowerCase().substring(1, txt.length); – SMA Oct 22 '16 at 18:50
  • @SMA that lowercases all letters except the first letter of the first word. I essentially do not want to not lowercase the first letter if it is uppercase already – jasan Oct 22 '16 at 18:54
  • Does this answer your question? [How do I make the first letter of a string uppercase in JavaScript?](https://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript) – Connor Low Aug 15 '22 at 15:06

4 Answers4

29

const upperCaseFirstLetter = string =>
 `${string.slice(0, 1).toUpperCase()}${string.slice(1)}`;

const lowerCaseAllWordsExceptFirstLetters = string =>
 string.replaceAll(/\S*/g, word =>
  `${word.slice(0, 1)}${word.slice(1).toLowerCase()}`
 );

const input = 'hello World, whatS up?';
const desiredOutput = upperCaseFirstLetter(lowerCaseAllWordsExceptFirstLetters(input));

console.log(desiredOutput);

Based on:

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

and

How to capitalize first letter of each word, like a 2-word city?

Chris Dąbrowski
  • 1,902
  • 1
  • 12
  • 12
  • I like what u did here but how about replacing the regex for function lowerCaseAllWordsExceptFirstLetters with this : /([^\s]+)/g so that it works for all words, I had issues with some words such as "İSTANBUL" , so I came up with this comment – Apak Dec 23 '19 at 12:21
28
word = word.charAt(0) + word.substring(1).toLowerCase();
Theo
  • 57,719
  • 8
  • 24
  • 41
  • 5
    Welcome to Stack Overflow. While your code may provide the answer to the question, please add context around it so others will have some idea what it does and why it is there. – Theo Aug 21 '19 at 20:28
  • 3
    @Theo Normally I would agree with what you wrote but this answer is pretty self-explanatory. – Paul Mar 11 '23 at 14:26
10

I was trying to do the same and I found a shorter way to do it:

function formatString(str) {
  return str
    .replace(/(\B)[^ ]*/g, match => (match.toLowerCase()))
    .replace(/^[^ ]/g, match => (match.toUpperCase()));
}

var text = "aaa BBB CCC";
console.log(formatString(text));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
G.Courmont
  • 187
  • 2
  • 10
  • Shorter isn't necessarily better; your answer is far less readable than the accepted answer. – Paul Mar 11 '23 at 14:23
4
function titleCase(str) {
  return str.split(' ').map(item => 
         item.charAt(0).toUpperCase() + item.slice(1).toLowerCase()).join(' ');
}

console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));

// It prints "Here Is My Handle Here Is My Spout";

Change the str parameter values and you are good to go.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131