Title case offer?

I am trying to write a string correctly in javascript - as long as I have this code: This does not seem to use the first letter, and I also went in cycles on how to scribble all letters after the first letter.

function titleCase(str) {
  var newstr = str.split(" ");
  for(i=0;i<newstr.length;i++){
    newstr[i].charAt(0).toUpperCase();

  }
   newstr = newstr.join(" ");
   return newstr;
}  

To be clear, I want every word in the sentence to be capitalized.

+4
source share
12 answers

That should work. Pay attention to how I set newstr[i]to the desired output. Functions such as .toUpperCase()do not affect the original string. They return only a new line with the required 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;
}  
+3
source

One of the cleanest ways I can come across using ES6, but not having the proper string prototype method .capitalize():

let sent = "these are just some words on paper"
sent.split(' ').map ( ([h, ...t]) => h.toUpperCase() + t.join('').toLowerCase() )

( ), , . s => s[0].toUpperCase() + s.substring(1).toLowerCase(), . , , , ES5, , :

function capitalize (sentence) {
    return sentence.split(' ').map(
        function (s) {
            return s[0].toUpperCase() + s.substring(1).toLowerCase()      
        }).join(' ') ;
}

, , .

+5

. :

newstr[i].charAt(0).toUpperCase();

, . , , , , , newstr[i].

function titleCase(str) {
  var newstr = str.split(" ");
  for(i=0;i<newstr.length;i++){
    newstr[i] = newstr[i].charAt(0).toUpperCase() + newstr[i].substring(1).toLowerCase();
  }
   newstr = newstr.join(" ");
   return newstr;
}
+3
function capitalizeFirstLetter(string) {
    return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}

.

, : JavaScript?

+2

Ramda, , :

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);

, ,

const capitalizeWords = map(s => concat(toUpper(head(s)), toLower(tail(s))));
const toTitleCase = compose(join(' '), capitalizeWords, split(' '));
+2
function titleCase(str) {
    var titleStr = str.split(' ');

    for (var i = 0; i < titleStr.length; i++) {

        titleStr[i] = titleStr[i].charAt(0).toUpperCase() + titleStr[i].slice(1).toLowerCase();

    }

    return titleStr.join(' ');
}

titleCase("i'm a little tea pot")
0

, . , :

function titleCase(str) {

return str.toLowerCase().replace(/^\w|\s\w/g, function(firstLetter) {
    return firstLetter.toUpperCase();
  });
}
titleCase("I'm a little tea pot");
0

function titleCase(str) {

var myArr = str.toLowerCase().split(" ");

for (var a = 0; a < myArr.length; a++){
     myArr[a] = myArr[a].charAt(0).toUpperCase() + myArr[a].substr(1);
  }

  return myArr.join(" ");
}
0
function titleCase(str) {
  var newArr = str.toLowerCase().split(" "), 
  firstLetter, 
  updArr = [];
  for(var i=0;i<newArr.length;i+=1){
    firstLetter = newArr[i].slice(0,1);
    updArr.push(newArr[i].replace(firstLetter, firstLetter.toUpperCase()));
  }
  return updArr.join(" ");
}
0

a function titleCase(string, array), , , . , . , , .

, ignore. .

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
0

First, go to lowercase, and then open each word, and then open each letter, the first letter, the tired capital, and then together

function titleCase(str) {
  var copy=str;
  copy=copy.toLowerCase();
  copy=copy.split(' ');
  for(var i=0;i<copy.length;i++){
    var cnt=copy[i].split('');
    cnt[0]=cnt[0].toUpperCase();
    copy[i]=cnt.join('');
    
    
  }
  str=copy.join(' ');
  return str;
}

titleCase("I'm a little tea pot");
Run codeHide result
0
source
function titleCase(str){
    var strToArray = str.split(" ");
    var newArray = [];       

    for(var i=0; i < strToArray.length; i++){
        var element = strToArray[i].replace(strToArray[i][0], strToArray[i][0].toUpperCase());
        newArray.push(element);
    }

    return (newArray.join(" "));
}
-1
source

Source: https://habr.com/ru/post/1598536/


All Articles