Reader Level:
Article

Transform Text From One Case to Another Using JavaScript

By Nitesh Luharuka on Apr 01, 2013
This article shows how to transform text from one case to another using JavaScript.

Friends,

This article will describe converting text from one case to another using JavaScript. In general programming, we need to convert 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 as in the following:

alert(toLowerCase("HELLO WORLD"));

To convert text into uppercase using JavaScript:

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

You can use this as in the following:

alert(toUpperCase("hello world"));

To convert 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 article. Let me know your thoughts and views via comments.

Nitesh Luharuka

Director at Rebin Infotech, C# Corner MVP for consecutive 3 years with over 6 years of experience in IT industry and exposure of working with wide variety of business applications. Particularly interested in Real-time Wi... Read more

COMMENT USING