blog

Home / DeveloperSection / Blogs / JavaScript - String Object

JavaScript - String Object

Danish Khan3856 20-Oct-2012
Introduction:

In this article I am going to explain about string object and what are the methods are available to perform manipulation on string.

String object in Java Script provides us work with a series of characters. The string object is used to manipulate a stored piece of text. String objects are created with the new keyword.

Syntax for creating string object:

var text=new String(“string”);

Or more simply we can declare string object like this

var text=”string”;

String Objects Properties:

Constructor: It will return the function that created the string object prototype.

Length: Returns the length of the string.

Prototype: Allows us to add properties and method to an object.

String Object Methods:

1.   charAt () : It will return the character at the specified index.
2.   charCodeAt() : It will return the number  which is the  actually the Unicode value of the character at the specified index.
3.   Concat() : It combine the two string into a single string.
4.   indexOf() : Returns the index position of the first occurrence of a specified value in a string.
5.  lastIndexOf() : Returns the last index position of a specified value in a string.
6.  match() : Used to match a given string with a regular expression.
7.  toString() : Returns the string representation of a specified object.
8.  toUpperCase() : Returns the call calling string value to uppercase.
9.   substring() : Returns a extracted substring between two indexes in the string.
10.   toLowerCase() : Returns the calling string value to lowercase.
11.   valueOf() : It returns the primitive value of a specified object.
12.   Split() : It splits the string object into array of two strings by separating the two strings into substrings.
13.   search() : To search a given pattern in a string.

String HTML Wrappers:

These are a list of methods which returns copy of the string wrapped inside the appropriate HTML tag.

1.    big() : It creates a string which is to be displayed in a big font same as it were in a big tag.

2.   blink() : It creates a string which provides the same function as the <blink> tag in that keeps blinking the text contained in tag.

3.   bold(): It creates a string as it were in the <b> tag.

4.   sub() : It is used to displays the string as a subscript same as the <sub> tag .

5.   sup() : It is used to display the string as a superscript same as the <sup> tag.

Example of Length Function in Java Script:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
        var countString = "Abhishek";
        document.write("Length of the string is : " + countString.length);
    </script>
</head>
<body>
</body>
</html>

Output will be:  Length of the string is : 8
Example of Split function used in Java Script:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
        var myString = "Abhishek";
        var mysplit = myString.split("i");
        document.write("The first element is " + mysplit[0] + "<br />" );
        document.write("The second element is " + mysplit[1]);
    </script>
</head>
<body>
</body>
</html>

 

In this example the split function will break the string into two parts
where the i is encountered      Output will be: The first element is Abh,

The second element is shek.
Example of Search Function:
When we have to search that a given expression is available in the given
string. If the match was successful then it will return the position where the
match was found else it will return -1.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
        var myregex = /Monday/;
        var myString = "Today is Monday";
        var IsMatchPosition = myString.search(myregex);
        if (IsMatchPosition != -1) {
            document.write("String matched at position " + IsMatchPosition);
        }
        else {
            document.write("String not matched");
        }
    </script>
</head>
<body>
</body>
</html>
String Replace Function:


String Replace function takes two argument:
1.    Search For: Search for contains what word is going to be replaced.

2.  Replace Text: It contains the words which will replace the Search for
Content.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
        var day = "Tuesday";
        var OldString = "Today is Monday";
        document.write("Old String is " + OldString);
        var NewString = OldString.replace("Monday", day);
        document.write("<br/>  New String is " + NewString);
    </script>
</head>
</html>

 Output is:

JavaScript - String Object

Example of IndexOf() Function:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
        var str = "is";
        var myString = "Today is Monday";
        var indexFound = myString.indexOf(str);
        document.write(indexFound);
    </script>
</head>
<body>
</body>
</html>

In this example I am showing the use of indexOf(), It will show the index
where

the first match is found.

JavaScript - String Object

Conclusion:     From this article you will know about the string functions
which are available in Java Script and using these functions what
operations can be performed on a given string.


Updated 18-Sep-2014

Leave Comment

Comments

Liked By