-6

I need to capitalize the first letter in a string, for example,

 sumo => Sumo 

and also need to capitalize the string with the following format,

Dr.ravi kumar => Dr.Ravi kumar

I am using a angular filter, which works well for the first condition but it doesn't work for the second one, this is the filter i am using,

 angular
    .module('app')
    .filter('capitalize', function () {
        return function (input, scope) {
            if (input != null) {
                input = input.toLowerCase();
                return input.substring(0, 1).toUpperCase() + input.substring(1);
            }
        }
    });
durai
  • 359
  • 1
  • 4
  • 12
  • You could split the String using the dot "."and the capitalize the first letter of both strings. – Manu Obre Jan 04 '17 at 12:57
  • How can the above code capitalize the `j` of `sumo joe`? – jensgram Jan 04 '17 at 12:58
  • 1
    Did you even try searching for this? – charlietfl Jan 04 '17 at 12:58
  • The code you're using really only transforms the first letter, it doesn't cover either of the two cases you listed. – fstanis Jan 04 '17 at 12:58
  • 1
    Possible duplicate of [How to capitalize first letter after period, question mark and exclamation in JavaScript?](http://stackoverflow.com/questions/39918994/how-to-capitalize-first-letter-after-period-question-mark-and-exclamation-in-ja) – fstanis Jan 04 '17 at 13:00
  • fstanis@ the link you mentioned doesn't satisfy the second condition i checked that already – durai Jan 04 '17 at 13:06
  • @durai In addition to the link fstanis linked, also check this out ► http://stackoverflow.com/questions/29044238/capitalizing-the-first-letter-after-a-tag together you should be able to parse it together I think. – Nope Jan 04 '17 at 13:09

2 Answers2

0
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

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
-1

Try this:

angular
    .module('app')
    .filter('capitalize', function () {
        return function (input, scope) {
            if (input != null) {
                input = input.toLowerCase();
                return input.substring(0, 1).toUpperCase() +input.substring(1).split('.')[0] + '.' + input.substring(1).split('.')[1].substring(0, 1).toUpperCase() + input.substring(1).split('.')[1].substring(1)
            }
        }
    });
CommonPlane
  • 145
  • 14