0

I have this string using JavaScript:

let str = "my city is beautiful."

I would like to get:

"My City Is Beautiful"

I tried this:

str.charAt(0).toUpperCase() + str.slice(1)

But I got:

My city is beautiful

Could you help me please?

meyi
  • 7,574
  • 1
  • 15
  • 28
teatzr
  • 59
  • 7
  • See this https://stackoverflow.com/questions/32589197/how-can-i-capitalize-the-first-letter-of-each-word-in-a-string-using-javascript – liontass Feb 02 '21 at 18:58
  • Also here https://www.digitalocean.com/community/tutorials/js-capitalizing-strings – Elvis Feb 02 '21 at 19:00

6 Answers6

2

you can use split, map and join:

"my city is beautiful"
  .split(" ")
  .map(s => s.charAt(0).toUpperCase() + s.slice(1))
  .join(" ")
peter554
  • 1,248
  • 1
  • 12
  • 24
1

Using map and join.

let str = "my city is beautiful."

var newStr = str.split(" ").map( str =>  str.substring(0,1).toUpperCase()+ str.substring(1)).join(' ')

console.log(newStr)

Note the logic here is:

  • Split each word
  • Iterate using map and, for each word: Set uppercase to first value and append the rest of the word using substring(1).
  • And join all values to get the phrase again.
J.F.
  • 13,927
  • 9
  • 27
  • 65
1
const string = "my city is beautiful.";
const words = string.split(" ");

for (let i = 0; i < words.length; i++) {
    words[i] = words[i][0].toUpperCase() + words[i].substr(1);
}

let newString = words.join(" ");
Jordan
  • 148
  • 7
1

You can:

  1. split up the string into words
  2. capitalize the first letter of each word
  3. join the words back into a single string

let str = "my city is beautiful."
const result =

  // split up the string into words
  str.split(' ')
  
  // capitalize the first letter of each word
  .map(word => {
    const [firstChar, ...rest] = word;
    return [firstChar.toUpperCase(), ...rest].join('');
  })
  
  // join the words back into a single string
  .join(' ');
  
console.log(result);
terrymorse
  • 6,771
  • 1
  • 21
  • 27
1

All you need to do is cycle through every word in your string and modify it individually. You can do that using split().

let str = "my city is beautiful."
let newStr = "" // Create temporary string

let words = str.split(" ") // Split the string into words and store them in array
for (word of words) { // Cycle through the array
    let newWord = word.charAt(0).toUpperCase() + word.slice(1) // Modify every word in the array
    newStr += (newWord + " ") // Append the word to the new String and add a space to the 
end
}

console.log(newStr.trim()) // Removes the spaces at the beginning and the end
// Output:
// "My City Is Beautiful."

Or a shorter approach:

let str = "my city is beautiful."
let newStr = ""

for (word of str.split(" "))
  newStr += (word.charAt(0).toUpperCase() + word.slice(1) + " ")

console.log(newStr.trim())
// Output:
// "My City Is Beautiful."
0

Learn how to here:

Basic Algorithm Scripting: Title Case a SentencePassed

freeCodeCamp Challenge Guide: Title Case a Sentence

let str = "my city is beautiful."

let newstr = str.toLowerCase().replace(/(^|\s)\S/g, L => L.toUpperCase());
console.log(newstr)
ikiK
  • 6,328
  • 4
  • 20
  • 40