capitalize the first letter and the letter followed by a dot(.) using angular js

string="hi you. im dr.test. nice to see you. by the way... what was your name?";
string=string.split("");
 up=true;
for(key in string){
 console.log(string[key],up);
  if(string[key]==="."){
    up=true;
  }else{
  if(up&&(string[key]!==" ")){
    string[key]=string[key].toUpperCase();
    up=false;
   }}}
  string=string.join("");
 console.log('result:',string);

My original answer (slow):

string="hi you. im dr.test. nice to see you";
words=string.split(" ");
var dots=[0];
for(key in words){//loop trough words
    var word=words[key];
    var chars=word.split("");
    if(dots.length){//there was a dot before, that wasnt a doctor: lets find the first letter +upper
       chars[0]=chars[0].toUpperCase();   
       dots.shift();//remove dot      
    }
   chars.forEach((char,i)=>{if(char=="."){dots.push(i)}});
   if(dots[0]&&dots[0]!=chars.length-1){//dot not at the end...
       chars[dots[0]+1]=chars[dots[0]+1].toUpperCase();//upper our doctor...
       dots.shift();//remove dot from array
   }
   word=chars.join("");
   words[key]=word;
}
 string=words.join(" ");
 console.log('result:',string);

Working example:
http://jsbin.com/docecuvote/edit?console

Leave a Comment