Property and function of string in JavaScript

Sourav.Kayal
Posted by in JavaScript category on for Beginner level | Points: 250 | Views : 1912 red flag
Rating: 4 out of 5  
 1 vote(s)

In this article we will learn various property and functions of string in JavaScript.

Property and function of string in JavaScript

In this article we will learn various property and functions of string object in JavaScript. As we know that JavaScript is object oriented language and everything is treated as object in JavaScript. String is also one type of object in JavaScript. It has only one property called “length” and few functions. We will understand them one by one with example.

Length property

Length property of string object returns the number of character it contains. Here is one example of length property of string.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
        <script>
            var a = "SouravKayal";
            alert(a.length);
        </script>
    </form>
</body>
</html>

This is the output of above example.


 

charAt() function

This function returns the character at specific position. It takes one integer argument which denoted index of string. Here is code snippet to understand charAt() function.

<body>
    <form id="form1" runat="server">
        <script>
            var a = "SouravKayal";
            alert(a.charAt(0));
        </script>
    </form>
</body>


IndexOf function

IndexOf() function returns the index of a particular character. In below example we are trying to find the index of ‘k’ from ‘a’ string.

<body>
    <form id="form1" runat="server">
        <script>
            var a = "SouravKayal";
            alert(a.indexOf('K'));
        </script>
    </form>
</body>

Here is sample output.


toLowerCase() function

This function is used to change the string to lower case from upper case. In example string there are two upper case characters and by using this function all characters are changed into lowercase.

<body>
    <form id="form1" runat="server">
        <script>
            var a = "SouravKayal";
            alert(a.toLowerCase());
        </script>
    </form>
</body>


 toUpperCase() function

This function is to make the string to uppercase.  This is sample example of toUpperCase() function.

<body>
    <form id="form1" runat="server">
        <script>
            var a = "SouravKayal";
            alert(a.toUpperCase());
        </script>
    </form>
</body>


 substring() function

Sometimes it is necessary to get the part of long string. In this situation we can use substring() fnction. Here is working example of substring() function.

<body>
    <form id="form1" runat="server">
        <script>
            var a = "SouravKayal";
            alert(a.substring(2,5));
        </script>
    </form>
</body>

We are seeing that it has given the part of string started by 2nd character


 

Replace() function

This function is used to replace some part of string by some other string. It uses pattern matching concept to replace the part of the string. In this example string we are replacing “Sourav” by “Replace” string.

<body>
    <form id="form1" runat="server">
        <script>
            var a = "SouravKayal";
            alert(a.replace("Sourav","Replaced"));
        </script>
    </form>
</body>


Conclusion:-

In this article we have seen various string handling function in JavaScript. Hope you understood the concept. Basically we can use those functions to validate user’s input.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 11/11/2013 | Points: 25
Good explanations.

Login to post response

Comment using Facebook(Author doesn't get notification)