Convert Capital letters to Small letters except the first letter

neharao
Giga Contributor

Hi all,

I have a requirement where i get values from excel sheet and using transform map i have to load it in servicenow. Can anyone please let me know how to convert all the capitals coming to smaller case except the first letter along with spaces. Please find below the format:'

For example:

ABCD EFGH IJKL

I want the output to look like below:

Abcd Efgh Ijkl

Kindly suggest me on this.

Thanks,

Neha

1 ACCEPTED SOLUTION

reginabautista
Kilo Sage

Try this:



var str = 'this is a test';


var pieces = str.split(" ");


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


      {


              var j = pieces[i].charAt(0).toUpperCase();


              pieces[i] = j + pieces[i].substr(1).toLowerCase();


      }




alert(pieces.join(" "));


View solution in original post

5 REPLIES 5

Chuck Tomasi
Tera Patron

What you're looking for is called Title case. You can use RegEx to do the replacement. Here's a function I found on StackOverflow.com.



function toTitleCase(str)
{
   
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}


Hi Chuck,



Thanks for the response. I too actually tried with this but this works only for one string but not after the one's with space.



Thanks,


Mithuna


reginabautista
Kilo Sage

Try this:



var str = 'this is a test';


var pieces = str.split(" ");


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


      {


              var j = pieces[i].charAt(0).toUpperCase();


              pieces[i] = j + pieces[i].substr(1).toLowerCase();


      }




alert(pieces.join(" "));


Hi Regina,



Thank you this worked



Thanks,


Neha