“Caso de título Uma frase” Respostas de código

Caso do título JavaScript

function titleCase(str) {
    return str
        .split(' ')
        .map((word) => word[0].toUpperCase() + word.slice(1).toLowerCase())
        .join(' ');
}
console.log(titleCase("I'm a little tea pot")); // I'm A Little tea Pot
Fancy Flatworm

Caso de título Uma frase

function titleCase(str) {
  const newTitle = str.split(" ");
  const updatedTitle = [];
  for (let st in newTitle) {
    updatedTitle[st] = newTitle[st][0].toUpperCase() + newTitle[st].slice(1).toLowerCase();
  }
  return updatedTitle.join(" ");
}

titleCase("I'm a little tea pot");

console.log(titleCase("I'm a little tea pot"))
console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"))
console.log(titleCase("sHoRt AnD sToUt"));

/*Code Explanation
Split the string by white spaces, and create a variable to track the updated title. 
Then we use a loop to turn turn the first character of the word to uppercase 
and the rest to lowercase. by creating concatenated string composed of the whole word 
in lowercase with the first character replaced by its uppercase. */

// Regex
function titleCase(str) {
  return str
    .toLowerCase()
    .replace(/(^|\s)\S/g, L => L.toUpperCase());
}

/* Code Explanation

The solution works by first lowercasing all the characters in the string and then only uppercasing the first character of each word.

    * Lowercase the whole string using str.toLowerCase().
    * Replace every word’ first character to uppercase using .replace.
    * Search for character at the beginning of each word i.e. matching any 
    character following a space or matching the first character of the 
    whole string, by using the following pattern.
    * Regex explanation:

    * Find all non-whitespace characters (\S)
    * At the beginning of string (^)
    * Or after any whitespace character (\s)

        - The g modifier searches for other such word pattern in the 
        whole string and replaces them.

        - This solution works with national symbols and accented letters 
        as illustrated by following examples
        international characters: 
        	‘бабушка курит трубку’ // → ‘Бабушка Курит Трубку’
        accented characters: 
        	‘località àtilacol’ // → ‘Località Àtilacol’
*/
Ralph Dizon

Respostas semelhantes a “Caso de título Uma frase”

Perguntas semelhantes a “Caso de título Uma frase”

Mais respostas relacionadas para “Caso de título Uma frase” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código