articles

Implementing String Object in Java Script

Anonymous User8056 14-Mar-2011

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

Implementing String Object in Java Script

When user clicks this button then he gets output.

Implementing String Object in Java Script



Updated 04-Mar-2020
I am a content writter !

Leave Comment

Comments

Liked By