How to transform text from one case to another using javascript

November 27th, 2012 | Posted by Nitesh Luharuka in General | Javascript | Utility

Friends,

This post will describe converting text from one case to another using javascript. In general programming, we need to convert a text to the following cases -

  • Lower Case
  • Upper Case
  • Title Case

We will see the conversion one by one below -

To convert a text into lowercase using Javascript

function toLowerCase(str) {
   return str.toLowerCase();
}

You can use this by calling -

alert toLowerCase("HELLO WORLD");

To convert a text into uppercase using Javascript

function toUpperCase(str) {
   return str.toUpperCase();
}

You can use this by calling -

alert toUpperCase("hello world");

To convert a text into Titlecase using Javascript

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

You can use this by calling -

alert toTitleCase("hello world");

Hope you all enjoyed reading this post. Let me know your thoughts and views via comments. :)

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

2 Responses



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Support us!

If you like this site please help and make click on any of these buttons!

×