Home > DeveloperSection > Articles > Implementing String Object in Java Script

Implementing String Object in Java Script


JavaScript JavaScript 
Ratings:
0 Comment(s)
 3950  View(s)
Rate this:

Implementing String Object in Java Script

The String object provides us some functionality to manipulate string according to requirement. We can use java script string function to concatenate to strings, to convert it in lower case and upper case, to find out substring and convert it to uppercase and lowercase. In this demonstration I will tell you a brief description about string function in java script.

Method and Property:

Ø  length  :  length property is used to find out the length of any string in java script.

Ø  toUpperCase()  :  toUpperCase() method is used to convert string in upper case letter.

Ø  toLowerCase()  :  toLowerCase() method is used to convert string in lower case letter.

Ø  charAt(n)  :  caharAt(n) function is used to find out character at nth position in string where n is an integer value.

Ø  substring(startIndex , endIndex )  :  substring() method is used to find out a substring in a given string. We can use startIndex and endIndex value to find out it.

An example which demonstrate use of string object in Java Script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

    <script type="text/javascript">

        function stringFunction() {

            var str = "John Corner";

            document.write("Length of the string is  :  " + str.length + "<br />");

            document.write("Upper case letter of the string is  :  " + str.toUpperCase() + "<br />");

            document.write("Lower case letter of the string is  :  " + str.toLowerCase() + "<br />");

            document.write("Value of Position of third character in str string is  :  " + str.charAt(3) + "<br />");

            document.write("Value from second position to fifth position is   :  " + str.substring(2, 5) + "<br />");

        }

    </script>

</head>

<body>

    <input type="button" value="Click this to view message" onclick="stringFunction()" />

</body>

</html>

Output of the following code snippet is as follows

String Object in Java Script

When user clicks this button then he gets output.

String Object in Java Script


Don't want to miss updates? Please click the below button!

Follow MindStick